# Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed

Given a string **str** containing only characters **0**, **1** and **2**, you can swap any two adjacent (consecutive) characters **0** and **1** or any two adjacent (consecutive) characters **1** and **2**. The task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times.

**Examples:**

Input:str = “100210”

Output:001120

We can swap 0 and 1 OR we can swap 1 and 2. Swapping 0 and 2 is not allowed. All the swaps can happen for adjacent only.

Input:str = “2021”

Output:1202

Note that 0 and 2 cannot be swapped

**Approach:** You can print all the **1s** together as **1** can be swapped with either of the other characters while **0** and **2** can not be swapped, so all the **0s** and **2s** will follow the same order as the original string.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the required string ` `void` `printString(string str, ` `int` `n) ` `{ ` ` ` `// count number of 1s ` ` ` `int` `ones = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(str[i] == ` `'1'` `) ` ` ` `ones++; ` ` ` ` ` `// To check if the all the 1s ` ` ` `// have been used or not ` ` ` `bool` `used = ` `false` `; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(str[i] == ` `'2'` `&& !used) { ` ` ` `used = 1; ` ` ` ` ` `// Print all the 1s if any 2 is encountered ` ` ` `for` `(` `int` `j = 0; j < ones; j++) ` ` ` `cout << ` `"1"` `; ` ` ` `} ` ` ` ` ` `// If str[i] = 0 or str[i] = 2 ` ` ` `if` `(str[i] != ` `'1'` `) ` ` ` `cout << str[i]; ` ` ` `} ` ` ` ` ` `// If 1s are not printed yet ` ` ` `if` `(!used) ` ` ` `for` `(` `int` `j = 0; j < ones; j++) ` ` ` `cout << ` `"1"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"100210"` `; ` ` ` `int` `n = str.length(); ` ` ` `printString(str, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

# Function to prthe required string

def printString(Str1, n):

# count number of 1s

ones = 0

for i in range(n):

if (Str1[i] == ‘1’):

ones += 1

# To check if the all the 1s

# have been used or not

used = False

for i in range(n):

if (Str1[i] == ‘2’ and used == False):

used = 1

# Prall the 1s if any 2 is encountered

for j in range(ones):

print(“1”, end = “”)

# If Str1[i] = 0 or Str1[i] = 2

if (Str1[i] != ‘1’):

print(Str1[i], end = “”)

# If 1s are not printed yet

if (used == False):

for j in range(ones):

print(“1”, end = “”)

# Driver code

Str1 = “100210”

n = len(Str1)

printString(Str1, n)

# This code is contributed

# by Mohit Kumar

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to print the required string ` `function` `printString(` `$str` `, ` `$n` `) ` `{ ` ` ` `// count number of 1s ` ` ` `$ones` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$str` `[` `$i` `] == ` `'1'` `) ` ` ` `$ones` `++; ` ` ` ` ` `// To check if the all the 1s ` ` ` `// have been used or not ` ` ` `$used` `= false; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$str` `[` `$i` `] == ` `'2'` `&& !` `$used` `) ` ` ` `{ ` ` ` `$used` `= 1; ` ` ` ` ` `// Print all the 1s if any 2 ` ` ` `// is encountered ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$ones` `; ` `$j` `++) ` ` ` `echo` `"1"` `; ` ` ` `} ` ` ` ` ` `// If str[i] = 0 or str[i] = 2 ` ` ` `if` `(` `$str` `[` `$i` `] != ` `'1'` `) ` ` ` `echo` `$str` `[` `$i` `]; ` ` ` `} ` ` ` ` ` `// If 1s are not printed yet ` ` ` `if` `(!` `$used` `) ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$ones` `; ` `$j` `++) ` ` ` `echo` `"1"` `; ` `} ` ` ` `// Driver code ` `$str` `= ` `"100210"` `; ` `$n` `= ` `strlen` `(` `$str` `); ` `printString(` `$str` `, ` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

001120

## Recommended Posts:

- Generate permutations with only adjacent swaps allowed
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest permutation of a string with given subsequences
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest string formed by removing at most one character
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Smallest length string with repeated replacement of two distinct adjacent
- Lexicographically smallest rotated sequence | Set 2
- Construct lexicographically smallest palindrome
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Make a lexicographically smallest palindrome with minimal changes
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest and largest substring of size k

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.