Given a string **str**, the task is to find the lexicographically smallest string that can be formed by removing at most one character from the given string.

Examples:

Input:str = "abcda"Output:abca One can remove 'd' to get "abca" which is the lexicographically smallest string possible.Input:str = "aaa'Output:aa

**Approach**: Traverse the string and delete the i-th character at the first point where **s[i]>s[i+1]**. If in case there is no such character then delete the last character in the string.

Below is the implementation of the above approach:

## C++

`// C++ program to find the lexicographically ` `// smallest string by removing at most one character ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the smallest string ` `string smallest(string s) ` `{ ` ` ` `int` `l = s.length(); ` ` ` `string ans = ` `""` `; ` ` ` ` ` `// iterate the string ` ` ` `for` `(` `int` `i = 0; i < l-1; i++) { ` ` ` ` ` `// first point where s[i]>s[i+1] ` ` ` `if` `(s[i] > s[i + 1]) { ` ` ` ` ` `// append the string without ` ` ` `// i-th character in it ` ` ` `for` `(` `int` `j = 0; j < l; j++) { ` ` ` `if` `(i != j) ` ` ` `ans += s[j]; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` `} ` ` ` ` ` `// leave the last character ` ` ` `ans = s.substr(0., l - 1); ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"abcda"` `; ` ` ` ` ` `cout << smallest(s); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the lexicographically ` `// smallest String by removing at most one character ` ` ` `class` `GFG { ` ` ` `// Function to return the smallest String ` ` ` `static` `String smallest(String s) { ` ` ` `int` `l = s.length(); ` ` ` `String ans = ` `""` `; ` ` ` ` ` `// iterate the String ` ` ` `for` `(` `int` `i = ` `0` `; i < l-` `1` `; i++) { ` ` ` ` ` `// first point where s[i]>s[i+1] ` ` ` `if` `(s.charAt(i) > s.charAt(i + ` `1` `)) { ` ` ` ` ` `// append the String without ` ` ` `// i-th character in it ` ` ` `for` `(` `int` `j = ` `0` `; j < l; j++) { ` ` ` `if` `(i != j) { ` ` ` `ans += s.charAt(j); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` `} ` ` ` ` ` `// leave the last character ` ` ` `ans = s.substring(` `0` `, l - ` `1` `); ` ` ` `return` `ans; ` ` ` `} ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `String s = ` `"abcda"` `; ` ` ` `System.out.println(smallest(s)); ` ` ` `} ` `} ` `/* This code is contributed by 29AjayKumar*/` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the lexicographically ` `# smallest string by removing at most one character ` ` ` `# Function to return the smallest string ` `def` `smallest(s): ` ` ` ` ` `l ` `=` `len` `(s) ` ` ` `ans ` `=` `"" ` ` ` ` ` `# iterate the string ` ` ` `for` `i ` `in` `range` `(l): ` ` ` ` ` `# first point where s[i]>s[i+1] ` ` ` `if` `(s[i] > s[i ` `+` `1` `]): ` ` ` ` ` `# append the string without ` ` ` `# i-th character in it ` ` ` `for` `j ` `in` `range` `(l): ` ` ` `if` `(i !` `=` `j): ` ` ` `ans ` `+` `=` `s[j] ` ` ` ` ` `return` `ans ` ` ` ` ` `# leave the last character ` ` ` `ans ` `=` `s[` `0` `: l ` `-` `1` `] ` ` ` `return` `ans ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"abcda"` ` ` ` ` `print` `(smallest(s)) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the lexicographically ` `// smallest String by removing at most ` `// one character ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// smallest String ` `static` `String smallest(String s) ` `{ ` ` ` `int` `l = s.Length; ` ` ` `String ans = ` `""` `; ` ` ` ` ` `// iterate the String ` ` ` `for` `(` `int` `i = 0; i < l-1; i++) ` ` ` `{ ` ` ` ` ` `// first point where s[i]>s[i+1] ` ` ` `if` `(s[i] > s[i + 1]) ` ` ` `{ ` ` ` ` ` `// append the String without ` ` ` `// i-th character in it ` ` ` `for` `(` `int` `j = 0; j < l; j++) ` ` ` `{ ` ` ` `if` `(i != j) ` ` ` `{ ` ` ` `ans += s[j]; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` `} ` ` ` ` ` `// leave the last character ` ` ` `ans = s.Substring(0, l - 1); ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `String s = ` `"abcda"` `; ` ` ` `Console.Write(smallest(s)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

abca

## Recommended Posts:

- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- 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
- Lexicographically smaller string by swapping at most one character pair
- Find lexicographically smallest string in at most one swaps
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Find a string such that every character is lexicographically greater than its immediate next character
- Count of strings that can be formed from another string using each character at-most once
- Minimize the length of string by removing occurrence of only one character
- Lexicographically smallest array after at-most K consecutive swaps
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimize ASCII values sum after removing all occurrences of one character
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Lexicographically largest string formed from the characters in range L and R
- Find longest palindrome formed by removing or shuffling chars from 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 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.