📌  相关文章
📜  字符串中使相邻字符不相等的最少替换次数

📅  最后修改于: 2021-09-06 06:12:18             🧑  作者: Mango

考虑到大小为N的小写字符字符串str。在一次操作中,可以将任何字符更改为其他字符。任务是找到最少的操作次数,使得没有两个相邻的字符相等。
例子:

方法:思路类似于实现滑动窗口技术。在这里,我们需要找到所有字符都相同的非重叠子串。那么最小的操作将是每个子串长度的一半的地板的总和。

  1. 无需直接更改字符。相反,考虑从只有一个字符的任何索引开始的所有子字符串。
  2. 现在考虑任何长度为l 的子串,使得该子串的所有字符都相等,然后将该子串的floor (l / 2) 个字符更改为某个其他字符。
  3. 因此,只要迭代所有从任何字符ch的字符串的字符找出这样的子串的最大长度,在这串中的所有字符都等于字符ch。
  4. 找到这个子串的长度 l 并将 floor ( l / 2) 添加到 ans
  5. 之后,从上述子字符串末尾旁边的字符开始。
C++14
// C++ program to find minimum
// replacements in a string to
// make adjacent characters unequal
 
#include 
using namespace std;
 
// Function which counts the minimum
// number of required operations
void count_minimum(string s)
{
    // n stores the length of the string s
    int n = s.length();
 
    // ans will store the required ans
    int ans = 0;
 
    // i is the current index in the string
    int i = 0;
 
    while (i < n) {
 
        int j = i;
 
        // Move j until characters s[i] & s[j]
        // are equal or the end of the
        // string is reached
        while (s[j] == s[i] && j < n) {
            j++;
        }
 
        // diff stores the length of the
        // substring such that all the
        // characters are equal in it
        int diff = j - i;
 
        // We need atleast diff/2 operations
        // for this substring
        ans += diff / 2;
        i = j;
    }
 
    cout << ans << endl;
}
 
// Driver code
int main()
{
    string str = "caaab";
    count_minimum(str);
    return 0;
}


Java
// Java program to find minimum
// replacements in a string to
// make adjacent characters unequal
import java.util.*;
 
class GFG{
 
// Function which counts the minimum
// number of required operations
static void count_minimum(String s)
{
     
    // n stores the length of the string s
    int n = s.length();
 
    // ans will store the required ans
    int ans = 0;
 
    // i is the current index in the string
    int i = 0;
 
    while (i < n)
    {
        int j = i;
 
        // Move j until characters s[i] & s[j]
        // are equal or the end of the
        // string is reached
        while (j < n && s.charAt(j) ==
                        s.charAt(i))
        {
            j++;
        }
 
        // diff stores the length of the
        // substring such that all the
        // characters are equal in it
        int diff = j - i;
 
        // We need atleast diff/2 operations
        // for this substring
        ans += diff / 2;
        i = j;
    }
    System.out.println(ans);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "caaab";
 
    count_minimum(str);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find minimum
# replacements in a string to
# make adjacent characters unequal
 
# Function which counts the minimum
# number of required operations
def count_minimum(s):
     
    # n stores the length of the string s
    n = len(s)
 
    # ans will store the required ans
    ans = 0
     
    # i is the current index in the string
    i = 0
     
    while i < n:
        j = i
         
        # Move j until characters s[i] & s[j]
        # are equal or the end of the
        # string is reached
        while j < n and (s[j] == s[i]):
            j += 1
         
        # diff stores the length of the
        # substring such that all the
        # characters are equal in it
        diff = j - i
         
        # We need atleast diff/2 operations
        # for this substring
        ans += diff // 2
        i = j
         
    print(ans)
     
# Driver code
if __name__=="__main__":
     
    str = "caaab"
    count_minimum(str)
     
# This code is contributed by rutvik_56


C#
// C# program to find minimum
// replacements in a string to
// make adjacent characters unequal
using System;
 
class GFG{
     
// Function which counts the minimum
// number of required operations
static void count_minimum(string s)
{
     
    // n stores the length of the string s
    int n = s.Length;
 
    // ans will store the required ans
    int ans = 0;
 
    // i is the current index in the string
    int i = 0;
 
    while (i < n)
    {
        int j = i;
 
        // Move j until characters s[i] & s[j]
        // are equal or the end of the
        // string is reached
        while (j < n && s[j] == s[i])
        {
            j++;
        }
 
        // diff stores the length of the
        // substring such that all the
        // characters are equal in it
        int diff = j - i;
 
        // We need atleast diff/2 operations
        // for this substring
        ans += diff / 2;
        i = j;
    }
    Console.WriteLine(ans);
}
 
// Driver code
static void Main()
{
    string str = "caaab";
     
    count_minimum(str);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出
1

时间复杂度: O (N)

辅助空间: O (1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live