Untitled
unknown
python
2 years ago
1.1 kB
12
Indexable
def findString(S, K): # stores minimum character N = len(S) minChar = 'z' # stores unique characters of # string in sorted order found = set([]) # Loop through to find the minimum character # and stores characters in the set for i in range(N): found.add(S[i]) if (S[i] < minChar): minChar = S[i] T = "" # Case 1: If N < K if (N < K): # copy the string S upto N characters T = S # append minChar to make the remaining # characters for i in range((K - N)): T += minChar # Case 2 : If N >= K else: T = "" # copy the string S upto K characters for i in range(K): T += S[i] T = list(T) # iterating in reverse direction for i in range(K - 1, -1, -1): # find the current character in the set if(T[i] in found): it = T[i] # increment the iterator it = chr(ord(it)+1) # check if some bigger character exists in set if (it in found): break # Replace current character with that found in set T[i] = it # Replace all characters after with minChar for j in range(i + 1, K): T[j] = minChar return T
Editor is loading...
Leave a Comment