Check if characters of a string can be made non-decreasing by replacing ‘?’s
Last Updated :
18 Jun, 2021
Given a string S of length N consisting of lowercase English alphabets and ‘?’, the task is to check if it is possible to make the string non-decreasing by replacing all the ‘?’ characters with English alphabets. If found to be true, print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “abb?xy?”
Output: Yes
Explanation:
Replacing the ‘?’s at index 3 and 6 with ‘b’ and ‘z’, modifies the string S to “abbbxyz”, which is non-decreasing.
Input: S = “??z?a?”
Output : No
Naive Approach: The simplest approach to solve the problem is to generate all possible strings using English alphabets and check if the resulting string is non-decreasing or not. If found to be true, print “Yes”, Otherwise, print “No”.
Time Complexity: O(N*26N)
Auxiliary Space: O(N)
Efficient Approach: The given problem can be solved based on the following observations:
- It can be observed that the only position that matters is the position with S[i]! = ‘?’, as one can replace the ‘?’ with the character at the nearest index that is not ‘?’.
- Therefore, the task is reduced to checking if characters of the strings that are not ‘?’, are in non-decreasing order or not.
Follow the steps below to solve the problem:
- Initialize a variable, say last, to store the last character which is not a ‘?’.
- Traverse the string and for every ith character, check if S[i]! = ‘?’ and S[i]<last, then print “No”.
- Otherwise, update last as last = S[i] if S[i]!=’?’.
- Print “Yes” if none of the above cases are satisfied.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int nonDecreasing(string s)
{
int size = s.length();
char c = 'a' ;
for ( int i = 0; i < size; i++) {
if (s[i] == '?' ) {
continue ;
}
else if ((s[i] != '?' )
&& (s[i] < c)) {
return 0;
}
else {
c = s[i];
}
}
return 1;
}
int main()
{
string S = "abb?xy?" ;
if (nonDecreasing(S))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int nonDecreasing( char []s)
{
int size = s.length;
char c = 'a' ;
for ( int i = 0 ; i < size; i++)
{
if (s[i] == '?' )
{
continue ;
}
else if ((s[i] != '?' )
&& (s[i] < c))
{
return 0 ;
}
else {
c = s[i];
}
}
return 1 ;
}
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" );
}
}
|
Python3
def nonDecreasing(s):
size = len (s)
c = 'a'
for i in range (size):
if (s[i] = = '?' ):
continue
elif ((s[i] ! = '?' ) and (s[i] < c)):
return 0
else :
c = s[i]
return 1
if __name__ = = '__main__' :
S = "abb?xy?"
if (nonDecreasing(S)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
static int nonDecreasing( char []s)
{
int size = s.Length;
char c = 'a' ;
for ( int i = 0; i < size; i++)
{
if (s[i] == '?' )
{
continue ;
}
else if ((s[i] != '?' )
&& (s[i] < c))
{
return 0;
}
else {
c = s[i];
}
}
return 1;
}
public static void Main(String[] args)
{
String S = "abb?xy?" ;
if (nonDecreasing(S.ToCharArray())==1)
Console.Write( "Yes" + "\n" );
else
Console.Write( "No" + "\n" );
}
}
|
Javascript
<script>
function nonDecreasing(s)
{
let size = s.length;
var c = 'a ';
// Traverse the String S
for(let 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
let S = "abb?xy?";
if (nonDecreasing(S.split(' ')))
document.write( "Yes" + "\n" );
else
document.write( "No" + "\n" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...