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

📅  最后修改于: 2021-10-26 05:24:15             🧑  作者: 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


Javascript


输出 :

geaksforgeaks

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程