The problem is math problems.
Assume, L is the number of char L, M is the number of char M, S is the number of char S.
After arrangement,
the first block should be "L" from 0 to L-1
the second block should be "M" from L to L+M-1
the third block should be "S" from L+M to L+M+S -1.
Assume g1 has the L, M, S number in the first block.
Assume g2 has the L, M, S number in the first block.
Assume g3 has the L, M, S number in the first block.
Their two case:
L , M and S are not 0, then
Switch one step
switch two steps.
2. L, M and S has one 0, then:
result is the block one's another number.
Code:
SS= input()
L = 0
M = 0
S = 0
all = [[0,0,0]]
for x in SS:
nL = all[-1][0]
nM = all[-1][1]
nS = all[-1][2]
if x == "L":
nL += 1
L += 1
elif x == "M":
nM += 1
M += 1
else:
nS += 1
S += 1
all.append([nL, nM, nS])
if L != 0 and M != 0 and S != 0:
g1 = all[L]
g2 = [all[L + M][0] - all[L][0], all[L + M][1] - all[L][1], all[L + M][2] - all[L][2]]
g3 = [all[L + M + S][0] - all[L + M][0], all[L + M + S][1] - all[L + M][1], all[L + M + S][2] - all[L + M][2]]
step = 0
step += min(g1[1], g2[0]) + min(g1[2], g3[0])
g1[1] -= min(g1[1], g2[0])
g2[0] -= min(g1[1], g2[0])
g1[2] -= min(g1[2], g3[0])
g3[0] -= min(g1[2], g3[0])
step += min(g2[2], g3[1])
g2[2] -= min(g2[2], g3[1])
g3[1] -= min(g2[2], g3[1])
step += g1[1]*2 + g1[2]*2
print(step)
else:
if L == 0:
if M != 0 and S != 0:
print(all[M][2])
else:
print(0)
elif M == 0:
if L != 0 and S != 0:
print(all[L][2])
else:
print(0)
else:
if L != 0 and M != 0:
print(all[M][1])
else:
print(0)
コメント