Transform a string such that it has abcd..z as a subsequence
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 appraoch.
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.
Recommended Posts:
- 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 number of times a string occurs as a subsequence in given string
- Find length of longest subsequence of one string which is substring of another 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
- Maximum number of removals of given subsequence from a string
- Longest subsequence of the form 0*1*0* in a binary string
- Longest subsequence with at least one character appearing in every string
- Find the lexicographically largest palindromic Subsequence of a String
- Minimum cost to make a string free of a subsequence
- Maximum length prefix of one string that occurs as subsequence in another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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



