📌  相关文章
📜  代替 '?'在一个字符串,这样没有两个相邻的字符是相同的

📅  最后修改于: 2021-09-07 03:10:34             🧑  作者: 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)

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