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.
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.
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.
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Transform string str1 into str2 by taking characters from string str3
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Transform One String to Another using Minimum Number of Given Operation
- Transform the string
- Check if it is possible to transform one string to another
- Minimum operations to transform given string to another by moving characters to front or end
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if an array has some palindromic subsequence of length at least 3
- Longest alternating subsequence which has maximum sum of elements
- Find the equal pairs of subsequence of S and subsequence of T
- Construct a string that has exactly K subsequences from given string
- Burrows - Wheeler Data Transform Algorithm
- Inverting the Burrows - Wheeler Transform
- Move To Front Data Transform Algorithm
- Inverting the Move to Front Transform
- Print an N x M matrix such that each row and column has all the vowels in it
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Minimum number of times A has to be repeated such that B is a substring of it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.