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.
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum replacements in a string to make adjacent characters unequal
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Rearrange characters in a string such that no two adjacent are same
- Find the character in first string that is present at minimum index in second string
- Print a closest string that does not contain adjacent duplicates
- C program to swap adjacent characters of a String
- Rearrange characters in a string such that no two adjacent are same using hashing
- Smallest length string with repeated replacement of two distinct adjacent
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- String with k distinct characters and no same characters adjacent
- Minimum sum of squares of character counts in a given string after removing k characters
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Queries to find the last non-repeating character in the sub-string of a given string
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.