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 appraoch.
Time Complexity: O(n), where n is the length of the string.
- Transform the string
- Check if it is possible to transform one string to another
- Transform One String to Another using Minimum Number of Given Operation
- Find length of longest subsequence of one string which is substring of another string
- Find number of times a string occurs as a subsequence in given string
- Queries on subsequence of string
- Given two strings, find if first string is a subsequence of second
- Count subsequence of length three in a given string
- Count All Palindromic Subsequence in a given String
- Longest subsequence of the form 0*1*0* in a binary string
- Maximum number of removals of given subsequence from a string
- Minimum cost to make a string free of a subsequence
- Maximum length prefix of one string that occurs as subsequence in another
- Find the lexicographically largest palindromic Subsequence of a String
- Inverting the Move to Front Transform
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : manishshaw1