Given a string S, the task is to find the minimum length string such that every adjacent character of the string remains adjacent in the minimum length string.
Input: S = “acabpba”
The given string can be converted to “pbac” in which,
every adjacent character remains adjacent.
Input: S = “abcdea”
It is impossible to find such string..
Approach: The idea is to prepare a graph like structure in which every adjacent nodes of the graph denotes the adjacent character of the string. There can be two cases in which such type of string is not possible –
- If a character contains three or more adjacent characters.
- If two characters do not have only one adjacent character, except in the case of string of length 1.
If the above conditions for a string are true, then simply traverse the graph with Depth First Search Traversal and the path of this traversal will be the minimum length string. Source vertex for the DFS will be any one of the characters with only one adjacent character.
Below is the implementation of the above approach:
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.
- Find a string such that every character is lexicographically greater than its immediate next character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Shortest distance to every other character from given character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Minimum K such that every substring of length atleast K contains a character c
- Lexicographically largest subsequence such that every character occurs at least k times
- Replace all occurrences of character X with character Y in given string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Longest subsequence with at least one character appearing in every string
- Reverse every word of the string except the first and the last character
- String formed with middle character of every right substring followed by left sequentially
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Rearrange the given string such that all prime multiple indexes have same character
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
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.