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.

**Examples:**

Input:S = “acabpba”Output:pbacExplanation:

The given string can be converted to “pbac” in which,

every adjacent character remains adjacent.

Input:S = “abcdea”Output:ImpossibleExplanation:

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:

`// C++ implementation to find the ` `// minimum length string such that ` `// adjacent characters of the string ` `// remains adjacent in the string ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `class` `graph { `
` ` `int` `arr[26][2]; `
` ` `vector<` `int` `> alpha; `
` ` `vector<` `int` `> answer; `
` ` `public` `: `
` ` `// Constructor for the graph `
` ` `graph() `
` ` `{ `
` ` `// Initialize the matrix by -1 `
` ` `for` `(` `int` `i = 0; i < 26; i++) { `
` ` `for` `(` `int` `j = 0; j < 2; j++) `
` ` `arr[i][j] = -1; `
` ` `} `
` ` ` ` `// Initialize the alphabet array `
` ` `alpha = vector<` `int` `>(26); `
` ` `} `
` ` ` ` `// Function to do Depth first `
` ` `// search Traversal `
` ` `void` `dfs(` `int` `v) `
` ` `{ `
` ` `// Pushing current character `
` ` `answer.push_back(v); `
` ` ` ` `alpha[v] = 1; `
` ` ` ` `for` `(` `int` `i = 0; i < 2; i++) { `
` ` `if` `(alpha[arr[v][i]] == 1 `
` ` `|| arr[v][i] == -1) `
` ` `continue` `; `
` ` ` ` `dfs(arr[v][i]); `
` ` `} `
` ` `} `
` ` ` ` `// Function to find the minimum `
` ` `// length string `
` ` `void` `minString(string str) `
` ` `{ `
` ` `// Condition if given string `
` ` `// length is 1 `
` ` `if` `(str.length() == 1) { `
` ` `cout << str << ` `"\n"` `; `
` ` `return` `; `
` ` `} `
` ` ` ` `bool` `flag = ` `true` `; `
` ` ` ` `// Loop to find the adjacency `
` ` `// list of the given string `
` ` `for` `(` `int` `i = 0; i < str.length() - 1; `
` ` `i++) { `
` ` `int` `j = str[i] - ` `'a'` `; `
` ` `int` `k = str[i + 1] - ` `'a'` `; `
` ` ` ` `// Condition if character `
` ` `// already present `
` ` `if` `(arr[j][0] == k `
` ` `|| arr[j][1] == k) { `
` ` `} `
` ` `else` `if` `(arr[j][0] == -1) `
` ` `arr[j][0] = k; `
` ` `else` `if` `(arr[j][1] == -1) `
` ` `arr[j][1] = k; `
` ` ` ` `// Condition if a character `
` ` `// have more than two different `
` ` `// adjacent characters `
` ` `else` `{ `
` ` `flag = ` `false` `; `
` ` `break` `; `
` ` `} `
` ` ` ` `if` `(arr[k][0] == j `
` ` `|| arr[k][1] == j) { `
` ` `} `
` ` `else` `if` `(arr[k][0] == -1) `
` ` `arr[k][0] = j; `
` ` `else` `if` `(arr[k][1] == -1) `
` ` `arr[k][1] = j; `
` ` ` ` `// Condition if a character `
` ` `// have more than two different `
` ` `// adjacent characters `
` ` `else` `{ `
` ` `flag = ` `false` `; `
` ` `break` `; `
` ` `} `
` ` `} `
` ` ` ` `// Variable to check string contain `
` ` `// two end characters or not `
` ` `bool` `contain_ends = ` `false` `; `
` ` ` ` `int` `count = 0; `
` ` `int` `index; `
` ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { `
` ` ` ` `// Condition if a character has `
` ` `// only one type of adjacent `
` ` `// character `
` ` `if` `((arr[i][0] == -1 `
` ` `&& arr[i][1] != -1) `
` ` `|| (arr[i][1] == -1 `
` ` `&& arr[i][0] != -1)) { `
` ` `count++; `
` ` `index = i; `
` ` `} `
` ` ` ` `// Condition if the given string `
` ` `// has exactly two characters `
` ` `// having only one type of adjacent `
` ` `// character `
` ` `if` `(count == 2) `
` ` `contain_ends = ` `true` `; `
` ` ` ` `if` `(count == 3) `
` ` `contain_ends = ` `false` `; `
` ` `} `
` ` ` ` `if` `(contain_ends == ` `false`
` ` `|| flag == ` `false` `) { `
` ` `cout << ` `"Impossible"`
` ` `<< ` `"\n"` `; `
` ` `return` `; `
` ` `} `
` ` ` ` `// Depth first Search Traversal `
` ` `// on one of the possible end `
` ` `// character of the string `
` ` `dfs(index); `
` ` ` ` `// Loop to print the answer `
` ` `for` `(` `int` `i = 0; i < answer.size(); `
` ` `i++) { `
` ` `char` `ch = answer[i] + ` `'a'` `; `
` ` `cout << ch; `
` ` `} `
` ` `} `
`}; ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `string s = ` `"abcdea"` `; `
` ` ` ` `graph g; `
` ` `g.minString(s); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

**Output:**

pbac

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:

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