📜  打印不包含相邻重复项的最接近的字符串

📅  最后修改于: 2021-04-29 14:46:53             🧑  作者: Mango

给定字符串S,更改S中最小的字母数,以使所有相邻字符都不同。打印结果字符串。

例子 :

Input : S = "aab"
Output: acb
Explanation :
Loop will start for i-th character, which 
is second ‘a’. It’s cannot be ‘b’ since it 
matches with third char. So output should
be ‘acb’.

Input : S = "geeksforgeeks"
Output: geaksforgeaks
Explanation :
Resultant string, after making minimal 
changes. S = "geaksforgeaks". We made two 
changes, which is the optimal solution here.

我们可以用贪婪的方法解决这个问题。让我们考虑一段连续相同字符的长度为k的段。我们必须在段中至少进行[K / 2]个更改,以确保一行中没有相同的字符。我们还可以改变第二,第四等。这是不应该等于在左边的字母和字母右侧字符串的字符。

从起始索引(i = 1)遍历字符串,如果相邻的两个字母(i&i-1)相等,则用’a’初始化第(i)个字符,并开始另一个循环以使第(i)个字符不同于左右字母。

下面是上述方法的实现:

C++
// C++ program to print a string with no adjacent
// duplicates by doing  minimum changes to original
// string
#include 
using namespace std;
  
// Function to print simple string
string noAdjacentDup(string s)
{
    int n = s.length();
    for (int i = 1; i < n; i++) 
    {
        // If any two adjacent characters are equal
        if (s[i] == s[i - 1]) 
        {
            s[i] = 'a'; // Initialize it to 'a'
  
            // Traverse the loop until it is different
            // from the left and right letter.
            while (s[i] == s[i - 1] || 
                (i + 1 < n && s[i] == s[i + 1]))             
                s[i]++;
              
            i++; 
        }
    }
    return s;
}
  
// Driver Function
int main()
{
    string s = "geeksforgeeks";
    cout << noAdjacentDup(s);
    return 0;
}


Java
// Java program to print a string with
// no adjacent duplicates by doing 
// minimum changes to original string
import java.util.*;
import java.lang.*;
  
public class GfG{
      
    // Function to print simple string
    public static String noAdjacentDup(String s1)
    {
        int n = s1.length();
        char[] s = s1.toCharArray();
        for (int i = 1; i < n; i++) 
        {
            // If any two adjacent 
            // characters are equal
            if (s[i] == s[i - 1]) 
            {
                // Initialize it to 'a'
                s[i] = 'a'; 
                  
                // Traverse the loop until it  
                // is different from the left
                // and right letter.
                while (s[i] == s[i - 1] || 
                    (i + 1 < n && s[i] == s[i + 1]))             
                    s[i]++;
              
                i++; 
            }
        }
        return (new String(s));
    }
      
    // Driver function
    public static void main(String argc[]){
  
        String s = "geeksforgeeks";
        System.out.println(noAdjacentDup(s));
          
    }
      
}
  
/* This code is contributed by Sagar Shukla */


Python3
# Python program to print a string with
# no adjacent duplicates by doing minimum 
# changes to original string
  
# Function to print simple string
def noAdjacentDup(s):
  
    n = len(s)
    for i in range(1, n): 
      
        # If any two adjacent characters are equal
        if (s[i] == s[i - 1]): 
          
            s[i] = "a" # Initialize it to 'a'
  
            # Traverse the loop until it is different
            # from the left and right letter.
            while (s[i] == s[i - 1] or
                (i + 1 < n and s[i] == s[i + 1])):             
                s[i] += 1
              
            i += 1
          
    return s
  
# Driver Function
s = list("geeksforgeeks")
print("".join(noAdjacentDup(s)))
  
# This code is contributed by Anant Agarwal.


C#
// C# program to print a string with
// no adjacent duplicates by doing 
// minimum changes to original string
using System;
  
class GfG{
      
    // Function to print simple string
    public static String noAdjacentDup(String s1)
    {
        int n = s1.Length;
          
        char[] s = s1.ToCharArray();
        for (int i = 1; i < n; i++) 
        {
            // If any two adjacent 
            // characters are equal
            if (s[i] == s[i - 1]) 
            {
                // Initialize it to 'a'
                s[i] = 'a'; 
                  
                // Traverse the loop until it 
                // is different from the left
                // and right letter.
                while (s[i] == s[i - 1] || 
                      (i + 1 < n && s[i] == s[i + 1]))             
                 s[i]++;
              
                i++; 
            }
        }
        return (new String(s));
    }
      
    // Driver function
    public static void Main(String[] argc)
    {
        String s = "geeksforgeeks";
          
        // Function calling
        Console.Write(noAdjacentDup(s));
    }
}
  
/* This code is contributed by parashar */


PHP


输出 :

geaksforgeaks