📌  相关文章
📜  代替 ‘?’在一个字符串,使得没有两个相邻的字符是相同的

📅  最后修改于: 2021-06-25 10:25:27             🧑  作者: Mango

给定长度为N的字符串S ,该字符串由“?”组成和小写字母,任务是替换“?”带有小写字母,因此没有相邻的字符是相同的。如果存在多个可能的组合,请打印其中的任何一种。

例子:

天真的方法:最简单的方法是尝试生成由小写字母组成的给定字符串的所有可能排列。可以有26个N字符串。在每个这些字符串,检查相邻字符是否匹配,并且给定字符串中的所有小写字符都与所选的字符串排列匹配。

时间复杂度: O(N * 26 N ),其中N是给定字符串的长度。
辅助空间: O(N)

高效的方法:为了优化上述方法,我们的想法是替换每个“?”。按字符“ a”,然后检查该字符是否等于相邻字符。如果它等于相邻字符,则增加当前字符。步骤如下:

  1. 如果字符串的第一个字符是“?”然后将其替换为“ a” ,如果它等于下一个字符,则将当前字符加1
  2. 使用变量i遍历[1,N – 1]范围内的给定字符串,如果当前字符为‘?’并执行以下操作:
    • 将索引i处的字符更新为s [i] = ‘a’
    • 现在,如果索引i和字符(I – 1)相同,则通过1递增当前字符。
    • 现在,如果在索引中的字符i第(i + 1)是相同的,然后由1递增当前字符。
    • 现在,如果在索引i(i – 1)的字符都一样了,再增加1当前字符。此步骤是强制性的,因为在上述步骤中的增量字符之后,可能索引i(i – 1)的字符相同。
  3. 如果字符串的最后一个字符是“?”然后将其替换为“ a” ,如果它等于前一个字符,则将最后一个字符加1
  4. 完成上述步骤后,打印字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function that replace all '?' with
// lowercase alphabets such that each
// adjacent character is different
string changeString(string S)
{
    // Store the given string
    string s = S;
 
    int N = (int)s.length();
 
    // If the first character is '?'
    if (s[0] == '?') {
        s[0] = 'a';
        if (s[0] == s[1]) {
            s[0]++;
        }
    }
 
    // Traverse the string [1, N - 1]
    for (int i = 1; i < N - 1; i++) {
 
        // If the current character is '?'
        if (s[i] == '?') {
 
            // Change the character
            s[i] = 'a';
 
            // Check equality with
            // the previous character
            if (s[i] == s[i - 1]) {
                s[i]++;
            }
 
            // Check equality with
            // the next character
            if (s[i] == s[i + 1]) {
                s[i]++;
            }
 
            // Check equality with
            // the previous character
            if (s[i] == s[i - 1]) {
                s[i]++;
            }
        }
    }
 
    // If the last character is '?'
    if (s[N - 1] == '?') {
 
        // Change character
        s[N - 1] = 'a';
 
        // Check with previous character
        if (s[N - 1] == s[N - 2]) {
            s[N - 1]++;
        }
    }
 
    // Return the resultant string
    return s;
}
 
// Driver Code
int main()
{
    // Given string S
    string S = "?a?a";
 
    // Function Call
    cout << changeString(S);
 
    return 0;
}


Java
// Java program for
// the above approach
class GFG{
 
// Function that replace all '?' with
// lowercase alphabets such that each
// adjacent character is different
static String changeString(String S)
{
  // Store the given String
  char []s = S.toCharArray();
 
  int N = (int)S.length();
 
  // If the first character is '?'
  if (s[0] == '?')
  {
    s[0] = 'a';
    if (s[0] == s[1])
    {
      s[0]++;
    }
  }
 
  // Traverse the String [1, N - 1]
  for (int i = 1; i < N - 1; i++)
  {
    // If the current
    // character is '?'
    if (s[i] == '?')
    {
      // Change the character
      s[i] = 'a';
 
      // Check equality with
      // the previous character
      if (s[i] == s[i - 1])
      {
        s[i]++;
      }
 
      // Check equality with
      // the next character
      if (s[i] == s[i + 1])
      {
        s[i]++;
      }
 
      // Check equality with
      // the previous character
      if (s[i] == s[i - 1])
      {
        s[i]++;
      }
    }
  }
 
  // If the last character is '?'
  if (s[N - 1] == '?')
  {
    // Change character
    s[N - 1] = 'a';
 
    // Check with previous
    // character
    if (s[N - 1] == s[N - 2])
    {
      s[N - 1]++;
    }
  }
 
  String ans = "";
   
  for(int  i = 0; i < s.length; i++)
  {
    ans += s[i];
  }
   
  // Return the resultant String
  return ans;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given String S
  String S = "?a?a";
 
  // Function Call
  System.out.print(changeString(S));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for
# the above approach
 
# Function that replace all '?' with
# lowercase alphabets such that each
# adjacent character is different
def changeString(S):
     
    # Store the given String
    N = len(S)
    s = [' '] * (len(S))
     
    for i in range(len(S)):
        s[i] = S[i]
 
    # If the first character is '?'
    if (s[0] == '?'):
        s[0] = 'a'
         
        if (s[0] == s[1]):
            s[0] = chr(ord(s[0]) + 1)
 
    # Traverse the String [1, N - 1]
    for i in range(1, N - 1):
         
        # If the current
        # character is '?'
        if (s[i] == '?'):
             
            # Change the character
            s[i] = 'a'
 
            # Check equality with
            # the previous character
            if (s[i] == s[i - 1]):
                s[i] =  chr(ord(s[i]) + 1)
 
            # Check equality with
            # the next character
            if (s[i] == s[i + 1]):
                s[i] =  chr(ord(s[i]) + 1)
 
            # Check equality with
            # the previous character
            if (s[i] == s[i - 1]):
                s[i] =  chr(ord(s[i]) + 1)
 
    # If the last character is '?'
    if (s[N - 1] == '?'):
         
        # Change character
        s[N - 1] = 'a'
         
        # Check with previous
        # character
        if (s[N - 1] == s[N - 2]):
            s[N - 1] += 1
 
    ans = ""
    for i in range(len(s)):
        ans += s[i]
         
    # Return the resultant String
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given String S
    S = "?a?a"
 
    # Function Call
    print(changeString(S))
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
  
class GFG{
 
// Function that replace all '?' with
// lowercase alphabets such that each
// adjacent character is different
static string changeString(string S)
{
   
  // Store the given String
  char []s = S.ToCharArray();
   
  int N = S.Length;
 
  // If the first character is '?'
  if (s[0] == '?')
  {
    s[0] = 'a';
    if (s[0] == s[1])
    {
      s[0]++;
    }
  }
 
  // Traverse the String [1, N - 1]
  for(int i = 1; i < N - 1; i++)
  {
     
    // If the current
    // character is '?'
    if (s[i] == '?')
    {
       
      // Change the character
      s[i] = 'a';
 
      // Check equality with
      // the previous character
      if (s[i] == s[i - 1])
      {
        s[i]++;
      }
 
      // Check equality with
      // the next character
      if (s[i] == s[i + 1])
      {
        s[i]++;
      }
 
      // Check equality with
      // the previous character
      if (s[i] == s[i - 1])
      {
        s[i]++;
      }
    }
  }
 
  // If the last character is '?'
  if (s[N - 1] == '?')
  {
     
    // Change character
    s[N - 1] = 'a';
 
    // Check with previous
    // character
    if (s[N - 1] == s[N - 2])
    {
      s[N - 1]++;
    }
  }
 
  string ans = "";
   
  for(int  i = 0; i < s.Length; i++)
  {
    ans += s[i];
  }
   
  // Return the resultant String
  return ans;
}
 
// Driver Code
public static void Main()
{
   
  // Given String S
  string S = "?a?a";
 
  // Function Call
  Console.WriteLine(changeString(S));
}
}
 
// This code is contributed by sanjoy_62


输出
baba







时间复杂度: O(N),其中N是给定字符串的长度。
辅助空间: O(N)