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 : abcdefghijklmnopqrstuvwxyzExplanation: 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; ` `} ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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) ` `?> ` |

*chevron_right*

*filter_none*

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.

## Recommended Posts:

- 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 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.