Given a string S of only small English letters. We need to transform the string by making some number of moves (any number of times) to get the string “abcdefghijklmnopqrstuvwxyz” as a subsequence in that string. In one move, you can replace any character of the string with the next character in alphabetical order i.e. ‘a’ can be replaced by ‘b’, ‘b’ can be replaced by ‘c’ and so on. Letter ‘z’ cannot be replaced by any character. If it is not possible to get the subsequence out of that string, then print “Not Possible”.
Note: The subsequence of a string is the string obtained by deleting some characters at some positions.
Examples:
Input : aaaaaaaaaaaaaaaaaaaaaaaaaa
Output : abcdefghijklmnopqrstuvwxyz
Explanation: Second occurrence of letter 'a' will be
replaced by 'b', third occurrence of letter 'a' will
be first replaced by 'b' and then by 'c' and so on.
Input : helloworld
Output : Not Possible
This problem can be solved using a greedy approach. The idea is to first observe that in a single move we can increment any character to next character only. That is, ‘c’ can be incremented to ‘d’,’e’,’f’ or any character greater than ‘c’. So we will create a variable ch, initially initialized to ‘a’ and by iterating over the string if we find the current character of the string not greater than ch, we’ll replace it with ch and increase the value of ch in alphabetical order. Since ch is initially equal to ‘a’, we’ll find ‘a’, replace it with ch, increment ch to store ‘b’ and move forward in the string, then if we find any character not greater than ‘b’ replace it with ‘b’ and again increment ch to store ‘c’ and move forward in the string. We will repeat these steps until ch reaches ‘z’ or the entire string is processed. If after traversing the whole string, ch does not reach ‘z’ then it is not possible to obtain the required subsequence.
Steps to solve this problem:
- Initialize ch to ‘a’.
- If the length of the input string s is less than 26, return false
- Iterate over the loop 0 to s.size()-1.
- If ch has reached ‘z’, Break the loop.
- If the current character in s is less than or equal to ch, replace it with ch and increment ch by one.
- If ch is less than or equal to ‘z’, Return false.
- Return true.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool transformString(string& s)
{
char ch = 'a' ;
if (s.size() < 26)
return false ;
for ( int i = 0; i < s.size(); i++) {
if ( int (ch) > int ( 'z' ))
break ;
if (s[i] <= ch) {
s[i] = ch;
ch = char ( int (ch) + 1);
}
}
if (ch <= 'z' )
return false ;
return true ;
}
int main()
{
string str = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
if (transformString(str))
cout << str << endl;
else
cout << "Not Possible" << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean transformString(StringBuilder s)
{
char ch = 'a' ;
if (s.length() < 26 )
return false ;
for ( int i = 0 ; i < s.length(); i++) {
if (( int )ch > ( int ) 'z' )
break ;
if (s.charAt(i) <= ch) {
s.setCharAt(i, ch);
ch = ( char )(( int )ch + 1 );
}
}
if (ch <= 'z' )
return false ;
return true ;
}
public static void main(String args[])
{
StringBuilder str =
new StringBuilder( "aaaaaaaaaaaaaaaaaaaaaaaaaa" );
if (transformString(str))
System.out.println(str.toString());
else
System.out.println( "Not Possible" );
}
}
|
Python3
def transformString(s) :
ch = 'a'
if ( len (s) < 26 ) :
return False
for i in range ( 0 , len (s)):
if ( ord (ch) > ord ( 'z' )) :
break
if (s[i] < = ch) :
s[i] = ch
ch = chr ( ord (ch) + 1 )
if (ch < = 'z' ) :
print ( "Not Possible" )
print ("".join(s))
s = list ( "aaaaaaaaaaaaaaaaaaaaaaaaaa" )
transformString(s)
|
C#
using System;
using System.Text;
using System.Collections.Generic;
class GFG {
static bool transformString( ref StringBuilder s)
{
char ch = 'a' ;
if (s.Length < 26)
return false ;
for ( int i = 0; i < s.Length; i++) {
if (( int )ch > 122)
break ;
if (s[i] <= ch) {
s[i] = ch;
ch = ( char )(( int )ch + 1);
}
}
if (ch <= 'z' )
return false ;
return true ;
}
public static void Main()
{
StringBuilder str = new
StringBuilder( "aaaaaaaaaaaaaaaaaaaaaaaaaa" );
if (transformString( ref str))
Console.WriteLine(str + "\n" );
else
Console.WriteLine( "Not Possible" + "\n" );
}
}
|
PHP
<?php
function transformString(& $s )
{
$ch = "a" ;
if ( strlen ( $s ) < 26)
return false;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if (ord( $ch ) > ord( "z" ))
break ;
if ( $s [ $i ] <= $ch )
{
$s [ $i ] = $ch ;
$ch = chr (ord( $ch ) + 1);
}
}
if ( $ch <= "z" )
return false;
return true;
}
$str = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
if (transformString( $str ))
echo $str ;
else
echo "Not Possible" ;
?>
|
Javascript
<script>
function transformString()
{
var ch = 'a' ;
if (s.length < 26)
return false ;
for ( var i = 0; i < s.length; i++) {
if (ch.charCodeAt(0) > 122)
break ;
if (s[i].charCodeAt(0) <= ch.charCodeAt(0)) {
s[i] = ch;
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
}
}
s = s.join(' ')
if (ch.charCodeAt(0) <= ' z '.charCodeAt(0))
return false;
return true;
}
// Driver Code
var s = "aaaaaaaaaaaaaaaaaaaaaaaaaa".split(' ');
if (transformString(s))
document.write(s);
else
document.write( "Not Possible" );
</script>
|
Outputabcdefghijklmnopqrstuvwxyz
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)