A string S is given. In one move, any two adjacent letters can be swapped. For example, given a string "abcd", it's possible to create "bacd", "acbd" or "abdc" in one such move. What is the lexicographically minimum string that can be achieved by at most K moves?
Write a function:
def solution(S, K)
that, given a string S of length N and an integer K, returns the lexicographically minimum string that can be achieved by at most K swaps of any adjacent letters.
Examples:
1. Given S = "decade" and K = 4, your function should return "adcede". Swaps could be:
decade → dceade,
dceade → dcaede,
dcaede → dacede,
dacede → adcede.
2. Given S = "bbbabbb" and K = 2, your function should return "babbbbb". The swaps are:
bbbabbb → bbabbbb,
bbabbbb → babbbbb.
3. Given S = "abracadabra" and K = 15, your function should return "aaaaabrcdbr".
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- string S consists only of lowercase letters ('a'-'z');
- K is an integer within the range [0..1,000,000,000].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
retu
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
return ''.join
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
return ''.join(S)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
return ''.join(S)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
return ''.join(S)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
S = list(S)
for i in range(len(S)):
pos = i
for j in range(i, len(S)):
if (j - i) > K:
break
if S[j] < S[pos]:
pos = j
for j in range(pos, i, -1):
S[j], S[j-1] = S[j-1], S[j]
K -= pos - i
return ''.join(S)
The following issues have been detected: timeout errors.
String sorted almost to the end, N = 80,000, K = 50,000.
Killed. Hard limit reached: 13.000 sec.
Most of the string consists of letters sorted in descending order, N = 80,000.
Killed. Hard limit reached: 14.000 sec.
Single repeating letter plus a short suffix.
Killed. Hard limit reached: 14.000 sec.