📌  相关文章
📜  检查是否可以制成一个字符串字符替换非递减“?的

📅  最后修改于: 2021-09-06 05:57:38             🧑  作者: Mango

给定一个长度为N的字符串S ,由小写英文字母和“?”组成,任务是检查是否有可能通过替换所有的‘?’使字符串不减少以英文字母字符。如果发现是真的,打印“是” 。否则,打印“否”

例子:

朴素的方法:解决问题的最简单方法是使用英文字母生成所有可能的字符串,并检查生成的字符串是否不减。如果发现是真的,打印“是” ,否则打印“否”。
时间复杂度: O(N*26 N )
辅助空间: O(N)

有效的方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个变量,比如last,来存储最后一个不是‘?’ 的字符
  • 遍历字符串,对于每个i字符,检查是否S[i]! = ‘?’S[i]然后打印“No”
  • 否则,如果S[i]!=’?’ ,则将last更新为last = S[i]
  • 如果以上情况都不满足,则打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if it's possible
// to make the string s non decreasing
int nonDecreasing(string s)
{
    // Stores length of string
    int size = s.length();
 
    // Stores the last character
    // that is not '?'
    char c = 'a';
 
    // Traverse the string S
    for (int i = 0; i < size; i++) {
 
        // If current character
        // of the string is '?'
        if (s[i] == '?') {
            continue;
        }
 
        // If s[i] is not '?'
        // and is less than C
        else if ((s[i] != '?')
                 && (s[i] < c)) {
 
            return 0;
        }
 
        // Otherwise
        else {
 
            // Update C
            c = s[i];
        }
    }
 
    // Return 1
    return 1;
}
 
// Driver Code
int main()
{
    string S = "abb?xy?";
 
    if (nonDecreasing(S))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to check if it's possible
// to make the String s non decreasing
static int nonDecreasing(char []s)
{
   
    // Stores length of String
    int size = s.length;
 
    // Stores the last character
    // that is not '?'
    char c = 'a';
 
    // Traverse the String S
    for (int i = 0; i < size; i++)
    {
 
        // If current character
        // of the String is '?'
        if (s[i] == '?')
        {
            continue;
        }
 
        // If s[i] is not '?'
        // and is less than C
        else if ((s[i] != '?')
                 && (s[i] < c))
        {
 
            return 0;
        }
 
        // Otherwise
        else {
 
            // Update C
            c = s[i];
        }
    }
 
    // Return 1
    return 1;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "abb?xy?";
 
    if (nonDecreasing(S.toCharArray())==1)
        System.out.print("Yes" +"\n");
    else
        System.out.print("No" +"\n");
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# python 3 program for the above approach
 
# Function to check if it's possible
# to make the string s non decreasing
def nonDecreasing(s):
   
    # Stores length of string
    size = len(s)
 
    # Stores the last character
    # that is not '?'
    c = 'a'
 
    # Traverse the string S
    for i in range(size):
       
        # If current character
        # of the string is '?'
        if (s[i] == '?'):
            continue
 
        # If s[i] is not '?'
        # and is less than C
        elif((s[i] != '?') and (s[i] < c)):
            return 0
 
        # Otherwise
        else:
           
            # Update C
            c = s[i]
 
    # Return 1
    return 1
 
# Driver Code
if __name__ == '__main__':
    S = "abb?xy?"
 
    if(nonDecreasing(S)):
        print("Yes")
    else:
        print("No")
         
        # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
  // Function to check if it's possible
  // to make the String s non decreasing
  static int nonDecreasing(char []s)
  {
 
    // Stores length of String
    int size = s.Length;
 
    // Stores the last character
    // that is not '?'
    char c = 'a';
 
    // Traverse the String S
    for (int i = 0; i < size; i++)
    {
 
      // If current character
      // of the String is '?'
      if (s[i] == '?')
      {
        continue;
      }
 
      // If s[i] is not '?'
      // and is less than C
      else if ((s[i] != '?')
               && (s[i] < c))
      {
 
        return 0;
      }
 
      // Otherwise
      else {
 
        // Update C
        c = s[i];
      }
    }
 
    // Return 1
    return 1;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    String S = "abb?xy?";
 
    if (nonDecreasing(S.ToCharArray())==1)
      Console.Write("Yes" +"\n");
    else
      Console.Write("No" +"\n");
 
  }
}
 
// This code is contributed by 29AjayKumar


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

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