📌  相关文章
📜  通过替换K对不同的相邻字符最小化字符串的长度

📅  最后修改于: 2021-04-27 21:25:22             🧑  作者: Mango

给定的字符串str的长度为N ,任务是找到最多可减少给定字符串的最小长度,方法是最多将K个字符替换为一对不相等的相邻字符。

例子:

天真的方法:最简单的方法是遍历字符串K次,并且在每次遍历期间,检查给定字符串的相邻字符对是否有区别。如果发现为真,则更换都与一个单个字符不等于与其相邻的字符的字符。最后,打印字符串的最小长度。

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

高效方法:为了优化上述方法,这个想法是遍历给定的字符串和检查是否有任何一对给定的字符串的相邻字符是不同的或没有。如果发现是真的,则打印max(1,(N-K))的值。否则,打印N。请按照以下步骤解决问题:

  1. 遍历给定的字符串str并检查是否有任何相邻的字符对是不同的。
  2. 如果发现是正确的,则输出max(1,(N-K))作为要求的答案。
  3. 否则,将N打印为所需答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to minimize the length
// of the string by replacing distinct
// pairs of adjacent characters
int MinLen(string str, int K)
{
    // Stores the length
    // of the given string
    int N = str.length();
 
    // Stores the index
    // of the given string
    int i = 0;
 
    // Traverse the given string
    while (i < N - 1) {
 
        // If two adjacent
        // characters are distinct
        if (str[i] != str[i + 1]) {
            break;
        }
        i++;
    }
 
    // If all characters
    // are equal
    if (i == N - 1) {
        return N;
    }
 
    // If all characters
    // are distinct
    return max(1, N - K);
}
 
// Driver Code
int main()
{
    string str = "aabc";
    int K = 1;
    cout << MinLen(str, K);
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to minimize the
// length of the String by
// replacing distinct pairs
// of adjacent characters
static int MinLen(String str,
                  int K)
{
  // Stores the length
  // of the given String
  int N = str.length();
 
  // Stores the index
  // of the given String
  int i = 0;
 
  // Traverse the given String
  while (i < N - 1)
  {
    // If two adjacent
    // characters are distinct
    if (str.charAt(i) !=
        str.charAt(i + 1))
    {
      break;
    }
    i++;
  }
 
  // If all characters
  // are equal
  if (i == N - 1)
  {
    return N;
  }
 
  // If all characters
  // are distinct
  return Math.max(1, N - K);
}
 
// Driver Code
public static void main(String[] args)
{
  String str = "aabc";
  int K = 1;
  System.out.print(MinLen(str, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to minimize the length
# of the by replacing distinct
# pairs of adjacent characters
def MinLen(str, K):
     
    # Stores the length
    # of the given string
    N = len(str)
 
    # Stores the index
    # of the given string
    i = 0
 
    # Traverse the given string
    while (i < N - 1):
 
        # If two adjacent
        # haracters are distinct
        if (str[i] != str[i + 1]):
            break
         
        i += 1
 
    # If all characters
    # are equal
    if (i == N - 1):
        return N
 
    # If all characters
    # are distinct
    return max(1, N - K)
 
# Driver Code
if __name__ == '__main__':
     
    str = "aabc"
    K = 1
     
    print(MinLen(str, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to minimize the
// length of the String by
// replacing distinct pairs
// of adjacent characters
static int MinLen(String str,
                  int K)
{
  // Stores the length
  // of the given String
  int N = str.Length;
 
  // Stores the index
  // of the given String
  int i = 0;
 
  // Traverse the given String
  while (i < N - 1)
  {
    // If two adjacent
    // characters are distinct
    if (str[i] != str[i + 1])
    {
      break;
    }
    i++;
  }
 
  // If all characters
  // are equal
  if (i == N - 1)
  {
    return N;
  }
 
  // If all characters
  // are distinct
  return Math.Max(1, N - K);
}
 
// Driver Code
public static void Main(String[] args)
{
  String str = "aabc";
  int K = 1;
  Console.Write(MinLen(str, K));
}
}
 
// This code is contributed by 29AjayKumar


输出:
3







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