📌  相关文章
📜  最小化要更改的字符,以使字符串的左右旋转相同

📅  最后修改于: 2021-04-27 20:24:27             🧑  作者: Mango

给定小写英文字母的字符串S ,任务是找到要更改的最小字符数,以使字符串的左右旋转相同。

例子:

做法:将重点观察要解决的问题是,当字符串的长度为偶数,则在奇数时,即使指数指数和字符的所有字符必须是同为左,右旋转是相同的。对于奇数长度的字符串,所有字符必须相等。请按照以下步骤解决问题:

  • 检查字符串的长度是否为偶数,则要更改的最小字符数是字符串的长度,不包括偶数索引和奇数索引处最常出现的元素的频率。
  • 否则,如果字符串的长度为奇数,则必须改变字符的最小数量是不包括该字符串中最发生字符的频率的字符串的长度。
  • 打印获得的最终计数。

下面是上述方法的实现:

C++
// C++ Program of the
// above approch
 
#include 
using namespace std;
 
// Function to find the minimum
// characters to be removed from
// the string
int getMinimumRemoval(string str)
{
    int n = str.length();
 
    // Initialize answer by N
    int ans = n;
 
    // If length is even
    if (n % 2 == 0) {
 
        // Frequency array for odd
        // and even indices
        vector freqEven(128);
        vector freqOdd(128);
 
        // Store the frequency of the
        // characters at even and odd
        // indices
        for (int i = 0; i < n; i++) {
            if (i % 2 == 0) {
                freqEven[str[i]]++;
            }
            else {
                freqOdd[str[i]]++;
            }
        }
 
        // Stores the most occuring frequency
        // for even and odd indices
        int evenMax = 0, oddMax = 0;
 
        for (char chr = 'a'; chr <= 'z'; chr++) {
 
            evenMax = max(evenMax, freqEven[chr]);
            oddMax = max(oddMax, freqOdd[chr]);
        }
 
        // Update the answer
        ans = ans - evenMax - oddMax;
    }
 
    // If length is odd
    else {
        // Stores the frequency of the
        // characters of the string
        vector freq(128);
        for (int i = 0; i < n; i++) {
            freq[str[i]]++;
        }
 
        // Stores the most occuring character
        // in the string
        int strMax = 0;
        for (char chr = 'a'; chr <= 'z'; chr++) {
            strMax = max(strMax, freq[chr]);
        }
 
        // Update the answer
        ans = ans - strMax;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    string str = "geeksgeeks";
 
    cout << getMinimumRemoval(str);
}


Java
// Java program of the
// above approch
class GFG{
     
// Function to find the minimum
// characters to be removed from
// the string
public static int getMinimumRemoval(String str)
{
    int n = str.length();
 
    // Initialize answer by N
    int ans = n;
 
    // If length is even
    if (n % 2 == 0)
    {
 
        // Frequency array for odd
        // and even indices
        int[] freqEven = new int[128];
        int[] freqOdd = new int[128];
 
        // Store the frequency of the
        // characters at even and odd
        // indices
        for(int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                freqEven[str.charAt(i)]++;
            }
            else
            {
                freqOdd[str.charAt(i)]++;
            }
        }
 
        // Stores the most occuring frequency
        // for even and odd indices
        int evenMax = 0, oddMax = 0;
 
        for(char chr = 'a'; chr <= 'z'; chr++)
        {
            evenMax = Math.max(evenMax,
                               freqEven[chr]);
            oddMax = Math.max(oddMax,
                              freqOdd[chr]);
        }
 
        // Update the answer
        ans = ans - evenMax - oddMax;
    }
 
    // If length is odd
    else
    {
         
        // Stores the frequency of the
        // characters of the string
        int[] freq = new int[128];
        for(int i = 0; i < n; i++)
        {
            freq[str.charAt(i)]++;
        }
 
        // Stores the most occuring character
        // in the string
        int strMax = 0;
        for(char chr = 'a'; chr <= 'z'; chr++)
        {
            strMax = Math.max(strMax, freq[chr]);
        }
         
        // Update the answer
        ans = ans - strMax;
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksgeeks";
     
    System.out.print(getMinimumRemoval(str));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 Program of the
# above approch
 
# Function to find the minimum
# characters to be removed from
# the string
def getMinimumRemoval(str):
   
    n = len(str)
 
    # Initialize answer by N
    ans = n
 
    # If length is even
    if(n % 2 == 0):
 
        # Frequency array for odd
        # and even indices
        freqEven = {}
        freqOdd = {}
        for ch in range(ord('a'),
                        ord('z') + 1):
            freqEven[chr(ch)] = 0
            freqOdd[chr(ch)] = 0
 
        # Store the frequency of the
        # characters at even and odd
        # indices
        for i in range(n):
            if(i % 2 == 0):
                if str[i] in freqEven:
                    freqEven[str[i]] += 1
            else:
                if str[i] in freqOdd:
                    freqOdd[str[i]] += 1
 
        # Stores the most occuring
        # frequency for even and
        # odd indices
        evenMax = 0
        oddMax = 0
 
        for ch in range(ord('a'),
                        ord('z') + 1):
            evenMax = max(evenMax,
                      freqEven[chr(ch)])
            oddMax = max(oddMax,
                     freqOdd[chr(ch)])
 
        # Update the answer
        ans = ans - evenMax - oddMax
 
    # If length is odd
    else:
 
        # Stores the frequency of the
        # characters of the string
        freq = {}
         
        for ch in range('a','z'):
            freq[chr(ch)] = 0
        for i in range(n):
            if str[i] in freq:
                freq[str[i]] += 1
 
        # Stores the most occuring
        # characterin the string
        strMax = 0
         
        for ch in range('a','z'):
            strMax = max(strMax,
                     freq[chr(ch)])
 
        # Update the answer
        ans = ans - strMax
 
    return ans
 
# Driver Code
str = "geeksgeeks"
print(getMinimumRemoval(str))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program of the
// above approch
using System;
class GFG{
     
// Function to find the minimum
// characters to be removed from
// the string
public static int getMinimumRemoval(String str)
{
  int n = str.Length;
 
  // Initialize answer by N
  int ans = n;
 
  // If length is even
  if (n % 2 == 0)
  {
    // Frequency array for odd
    // and even indices
    int[] freqEven = new int[128];
    int[] freqOdd = new int[128];
 
    // Store the frequency of the
    // characters at even and odd
    // indices
    for(int i = 0; i < n; i++)
    {
      if (i % 2 == 0)
      {
        freqEven[str[i]]++;
      }
      else
      {
        freqOdd[str[i]]++;
      }
    }
 
    // Stores the most occuring frequency
    // for even and odd indices
    int evenMax = 0, oddMax = 0;
 
    for(char chr = 'a'; chr <= 'z'; chr++)
    {
      evenMax = Math.Max(evenMax,
                         freqEven[chr]);
      oddMax = Math.Max(oddMax,
                        freqOdd[chr]);
    }
 
    // Update the answer
    ans = ans - evenMax - oddMax;
  }
 
  // If length is odd
  else
  {
    // Stores the frequency of the
    // characters of the string
    int[] freq = new int[128];
    for(int i = 0; i < n; i++)
    {
      freq[str[i]]++;
    }
 
    // Stores the most occuring character
    // in the string
    int strMax = 0;
    for(char chr = 'a'; chr <= 'z'; chr++)
    {
      strMax = Math.Max(strMax, freq[chr]);
    }
 
    // Update the answer
    ans = ans - strMax;
  }
  return ans;
}
 
// Driver code
public static void Main(String[] args)
{
  String str = "geeksgeeks";
  Console.Write(getMinimumRemoval(str));
}
}
 
// This code is contributed by Rajput-Ji


输出:
6






时间复杂度: O(N)
辅助空间: O(1)