Lexicographically smallest string formed by removing at most one character - GeeksforGeeks

24 May, 2021
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++
Java
Python3
C#
Javascript
// 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 stringstring 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 Codeint main(){    string s = "abcda";     cout << smallest(s);     return 0;}
// 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*/
# Python3 program to find the lexicographically# smallest string by removing at most one character # Function to return the smallest stringdef smallest(s):     l = len(s)    ans = ""     # iterate the string    for i in range (l-1):         # 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 Codeif __name__ == "__main__":     s = "abcda"     print (smallest(s)) # This code is contributed by ita_c
// C# program to find the lexicographically// smallest String by removing at most// one characterusing System; class GFG{ // Function to return the// smallest Stringstatic 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 Codepublic static void Main(){    String s = "abcda";    Console.Write(smallest(s));}} // This code is contributed by 29AjayKumar
<script>// Javascript program to find the lexicographically// smallest String by removing at most one character         // Function to return the smallest String    function smallest(s)    {        let l = s.length;        let ans = "";          // iterate the String        for (let 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 (let 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    let s = "abcda";    document.write(smallest(s)); // This code is contributed by rag2127</script>
abca
29AjayKumar
ukasp
atishayjain13
recordable0711
rag2127
lexicographic-ordering
Technical Scripter 2018
Competitive Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bits manipulation (Important tactics)
Breadth First Traversal ( BFS ) on a 2D array
Remove all occurrences of a character in a string | Recursive approach
Container with Most Water
How to begin with Competitive Programming?
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types