Suppose we have a string s. We have to find all possible combinations of letters of s. If there are two strings with same set of characters, then show the lexicographically smallest of them. And one constraint is each character in s are unique.
So, if the input is like s = "pqr", then the output will be ['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(s): st_arr =  for i in range(len(s)-1,-1,-1): for j in range(len(st_arr)): st_arr.append(s[i]+st_arr[j]) st_arr.append(s[i]) return st_arr s = "pqr" print(solve(s))
['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']