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
1def smallest_string(s: str) -> str:
2 for i in range(len(s) - 1):
3 if s[i] > s[i + 1]:
4 break
5 return s[:i] + s[i + 1:]
6
7if __name__ == '__main__':
8 s = input()
9 res = smallest_string(s)
10 print(res)
11