Microsoft Online Assessment (OA) - Lexicographically Smallest String

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.

Example 1:

Input: abczd

Output: abcd

Example 2:

Input: abcda

Output: abca

Explanation:

One can remove d to get abca which is the lexicographically smallest string possible.

Try it yourself

Implementation

1
1
def smallest_string(s: str) -> str:
2
-
    # WRITE YOUR BRILLIANT CODE HERE
2
+
    for i in range(len(s) - 1):
3
-
    return ''
3
+
        if s[i] > s[i + 1]:
4
+
            break
5
+
    return s[:i] + s[i + 1:]
6
+
4
7
if __name__ == '__main__':
5
8
    s = input()
6
9
    res = smallest_string(s)
7
10
    print(res)