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 to 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: 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.
Below is the implementation of the above approach.
C++
// C Plus Plus Program to transform the // given string to contain the required // subsequence #include <bits/stdc++.h> using namespace std; // function to transform string with string // passed as reference bool transformString(string& s) { // initializing the variable ch to 'a' char ch = 'a' ; // if the length of string is less than // 26, we can't obtain the required // subsequence if (s.size() < 26) return false ; for ( int i = 0; i < s.size(); i++) { // if ch has reached 'z', it means we have // transformed our string such that required // subsequence can be obtained if ( int (ch) > int ( 'z' )) break ; // current character is not greater than ch, // then replace it with ch and increment ch if (s[i] <= ch) { s[i] = ch; ch = char ( int (ch) + 1); } } if (ch <= 'z' ) return false ; return true ; } // Driver Code int main() { string str = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ; if (transformString(str)) cout << str << endl; else cout << "Not Possible" << endl; return 0; } |
Java
// Java Program to transform the given string // to contain the required subsequence import java.io.*; public class GFG { // function to transform string with // string passed as reference static boolean transformString(StringBuilder s) { // initializing the variable ch to 'a' char ch = 'a' ; // if the length of string is less than // 26, we can't obtain the required // subsequence if (s.length() < 26 ) return false ; for ( int i = 0 ; i < s.length(); i++) { // if ch has reached 'z', it means // we have transformed our string // such that required subsequence // can be obtained if (( int )ch > ( int ) 'z' ) break ; // current character is not greater // than ch, then replace it with // ch and increment ch if (s.charAt(i) <= ch) { s.setCharAt(i, ch); ch = ( char )(( int )ch + 1 ); } } if (ch <= 'z' ) return false ; return true ; } // Driver Code 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" ); } } // This code is contributed by Manish Shaw // (manishshaw1) |
Python3
# Python3 Program to transform the # given string to contain the required # subsequence # function to transform string with string # passed as reference def transformString(s) : # initializing the variable ch to 'a' ch = 'a' # if the length of string is less than # 26, we can't obtain the required # subsequence if ( len (s) < 26 ) : return False for i in range ( 0 , len (s)): # if ch has reached 'z', it means we have # transformed our string such that required # subsequence can be obtained if ( ord (ch) > ord ( 'z' )) : break # current character is not greater than ch, # then replace it with ch and increment ch if (s[i] < = ch) : s[i] = ch ch = chr ( ord (ch) + 1 ) if (ch < = 'z' ) : print ( "Not Possible" ) print ("".join(s)) # Driver Code s = list ( "aaaaaaaaaaaaaaaaaaaaaaaaaa" ) transformString(s) # This code is contributed by Manish Shaw # (manishshaw1) |
C#
// C# Program to transform the given string // to contain the required subsequence using System; using System.Text; using System.Collections.Generic; class GFG { // function to transform string with string // passed as reference static bool transformString( ref StringBuilder s) { // initializing the variable ch to 'a' char ch = 'a' ; // if the length of string is less than // 26, we can't obtain the required // subsequence if (s.Length < 26) return false ; for ( int i = 0; i < s.Length; i++) { // if ch has reached 'z', it means we // have transformed our string such // that required subsequence can be // obtained if (( int )ch > 122) break ; // current character is not greater // than ch, then replace it with ch // and increment ch if (s[i] <= ch) { s[i] = ch; ch = ( char )(( int )ch + 1); } } if (ch <= 'z' ) return false ; return true ; } // Driver Code public static void Main() { StringBuilder str = new StringBuilder( "aaaaaaaaaaaaaaaaaaaaaaaaaa" ); if (transformString( ref str)) Console.WriteLine(str + "\n" ); else Console.WriteLine( "Not Possible" + "\n" ); } } // This code is contributed by Manish Shaw // (manishshaw1) |
PHP
<?php // PHP Program to transform the // given string to contain the required // subsequence // function to transform string with // string passed as reference function transformString(& $s ) { // initializing the variable // ch to 'a' $ch = "a" ; // if the length of string is less than // 26, we can't obtain the required // subsequence if ( strlen ( $s ) < 26) return false; for ( $i = 0; $i < strlen ( $s ); $i ++) { // if ch has reached 'z', // it means we have // transformed our string // such that required // subsequence can be obtained if (ord( $ch ) > ord( "z" )) break ; // current character is not // greater than ch, then // replace it with ch and // increment ch if ( $s [ $i ] <= $ch ) { $s [ $i ] = $ch ; $ch = chr (ord( $ch ) + 1); } } if ( $ch <= "z" ) return false; return true; } // Driver Code $str = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ; if (transformString( $str )) echo $str ; else echo "Not Possible" ; // This code is contributed by // Manish Shaw (manishshaw1) ?> |
Output:
abcdefghijklmnopqrstuvwxyz
Time Complexity: O(n), where n is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.