Skip to content

Commit 0fe433a

Browse files
Implement findLexSmallestString method in Solution class
1 parent 10a0de1 commit 0fe433a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

findLexSmallestString.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def findLexSmallestString(self, s: str, a: int, b: int) -> str:
3+
q = deque([s])
4+
vis = {s}
5+
ans = s
6+
while q:
7+
s = q.popleft()
8+
if ans > s:
9+
ans = s
10+
t1 = ''.join(
11+
[str((int(c) + a) % 10) if i & 1 else c for i, c in enumerate(s)]
12+
)
13+
t2 = s[-b:] + s[:-b]
14+
for t in (t1, t2):
15+
if t not in vis:
16+
vis.add(t)
17+
q.append(t)
18+
      return ans

0 commit comments

Comments
 (0)