Skip to content

Commit f93a912

Browse files
committed
feat: add solutions to lc problem: No.1625
No.1625.Lexicographically Smallest String After Applying Operations
1 parent 4a96974 commit f93a912

File tree

6 files changed

+334
-2
lines changed

6 files changed

+334
-2
lines changed

solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/README.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,136 @@
8383

8484
<!-- 这里可写通用的实现逻辑 -->
8585

86+
**方法一:BFS**
87+
8688
<!-- tabs:start -->
8789

8890
### **Python3**
8991

9092
<!-- 这里可写当前语言的特殊实现逻辑 -->
9193

9294
```python
93-
95+
class Solution:
96+
def findLexSmallestString(self, s: str, a: int, b: int) -> str:
97+
q = deque([s])
98+
vis = {s}
99+
ans = s
100+
while q:
101+
s = q.popleft()
102+
if s < ans:
103+
ans = s
104+
nxt1 = ''.join(
105+
[str((int(c) + a) % 10) if i & 1 else c for i, c in enumerate(s)]
106+
)
107+
nxt2 = s[-b:] + s[:-b]
108+
for nxt in (nxt1, nxt2):
109+
if nxt not in vis:
110+
vis.add(nxt)
111+
q.append(nxt)
112+
return ans
94113
```
95114

96115
### **Java**
97116

98117
<!-- 这里可写当前语言的特殊实现逻辑 -->
99118

100119
```java
120+
class Solution {
121+
public String findLexSmallestString(String s, int a, int b) {
122+
Queue<String> q = new ArrayDeque<>();
123+
q.offer(s);
124+
Set<String> vis = new HashSet<>();
125+
vis.add(s);
126+
String ans = s;
127+
while (!q.isEmpty()) {
128+
s = q.poll();
129+
if (s.compareTo(ans) < 0) {
130+
ans = s;
131+
}
132+
char[] cs = s.toCharArray();
133+
for (int i = 1; i < cs.length; i += 2) {
134+
cs[i] = (char) (((cs[i] - '0' + a) % 10) + '0');
135+
}
136+
String nxt1 = String.valueOf(cs);
137+
String nxt2 = s.substring(b) + s.substring(0, b);
138+
for (String nxt : new String[]{nxt1, nxt2}) {
139+
if (!vis.contains(nxt)) {
140+
vis.add(nxt);
141+
q.offer(nxt);
142+
}
143+
}
144+
}
145+
return ans;
146+
}
147+
}
148+
```
149+
150+
### **C++**
151+
152+
```cpp
153+
class Solution {
154+
public:
155+
string findLexSmallestString(string s, int a, int b) {
156+
unordered_set<string> vis{{s}};
157+
queue<string> q{{s}};
158+
string ans = s;
159+
int n = s.size();
160+
while (!q.empty())
161+
{
162+
s = q.front();
163+
q.pop();
164+
if (s < ans) ans = s;
165+
string nxt1 = s;
166+
for (int i = 1; i < n; i += 2) nxt1[i] = ((nxt1[i] - '0' + a) % 10) + '0';
167+
string nxt2 = s.substr(n - b) + s.substr(0, n - b);
168+
for (string nxt : {nxt1, nxt2})
169+
{
170+
if (!vis.count(nxt))
171+
{
172+
vis.insert(nxt);
173+
q.push(nxt);
174+
}
175+
}
176+
}
177+
return ans;
178+
}
179+
};
180+
```
101181
182+
### **Go**
183+
184+
```go
185+
func findLexSmallestString(s string, a int, b int) string {
186+
q := []string{s}
187+
vis := map[string]bool{s: true}
188+
ans := s
189+
for len(q) > 0 {
190+
s = q[0]
191+
q = q[1:]
192+
if s < ans {
193+
ans = s
194+
}
195+
for _, nxt := range []string{op1(s, a), op2(s, b)} {
196+
if !vis[nxt] {
197+
vis[nxt] = true
198+
q = append(q, nxt)
199+
}
200+
}
201+
}
202+
return ans
203+
}
204+
205+
func op1(s string, a int) string {
206+
res := []byte(s)
207+
for i := 1; i < len(s); i += 2 {
208+
res[i] = byte((int(res[i]-'0')+a)%10 + '0')
209+
}
210+
return string(res)
211+
}
212+
213+
func op2(s string, b int) string {
214+
return s[len(s)-b:] + s[:len(s)-b]
215+
}
102216
```
103217

104218
### **...**

solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/README_EN.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,132 @@ There is no way to obtain a string that is lexicographically smaller then &quot;
7070

7171
## Solutions
7272

73+
BFS.
74+
7375
<!-- tabs:start -->
7476

7577
### **Python3**
7678

7779
```python
78-
80+
class Solution:
81+
def findLexSmallestString(self, s: str, a: int, b: int) -> str:
82+
q = deque([s])
83+
vis = {s}
84+
ans = s
85+
while q:
86+
s = q.popleft()
87+
if s < ans:
88+
ans = s
89+
nxt1 = ''.join(
90+
[str((int(c) + a) % 10) if i & 1 else c for i, c in enumerate(s)]
91+
)
92+
nxt2 = s[-b:] + s[:-b]
93+
for nxt in (nxt1, nxt2):
94+
if nxt not in vis:
95+
vis.add(nxt)
96+
q.append(nxt)
97+
return ans
7998
```
8099

81100
### **Java**
82101

83102
```java
103+
class Solution {
104+
public String findLexSmallestString(String s, int a, int b) {
105+
Queue<String> q = new ArrayDeque<>();
106+
q.offer(s);
107+
Set<String> vis = new HashSet<>();
108+
vis.add(s);
109+
String ans = s;
110+
while (!q.isEmpty()) {
111+
s = q.poll();
112+
if (s.compareTo(ans) < 0) {
113+
ans = s;
114+
}
115+
char[] cs = s.toCharArray();
116+
for (int i = 1; i < cs.length; i += 2) {
117+
cs[i] = (char) (((cs[i] - '0' + a) % 10) + '0');
118+
}
119+
String nxt1 = String.valueOf(cs);
120+
String nxt2 = s.substring(b) + s.substring(0, b);
121+
for (String nxt : new String[]{nxt1, nxt2}) {
122+
if (!vis.contains(nxt)) {
123+
vis.add(nxt);
124+
q.offer(nxt);
125+
}
126+
}
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
string findLexSmallestString(string s, int a, int b) {
139+
unordered_set<string> vis{{s}};
140+
queue<string> q{{s}};
141+
string ans = s;
142+
int n = s.size();
143+
while (!q.empty())
144+
{
145+
s = q.front();
146+
q.pop();
147+
if (s < ans) ans = s;
148+
string nxt1 = s;
149+
for (int i = 1; i < n; i += 2) nxt1[i] = ((nxt1[i] - '0' + a) % 10) + '0';
150+
string nxt2 = s.substr(n - b) + s.substr(0, n - b);
151+
for (string nxt : {nxt1, nxt2})
152+
{
153+
if (!vis.count(nxt))
154+
{
155+
vis.insert(nxt);
156+
q.push(nxt);
157+
}
158+
}
159+
}
160+
return ans;
161+
}
162+
};
163+
```
84164
165+
### **Go**
166+
167+
```go
168+
func findLexSmallestString(s string, a int, b int) string {
169+
q := []string{s}
170+
vis := map[string]bool{s: true}
171+
ans := s
172+
for len(q) > 0 {
173+
s = q[0]
174+
q = q[1:]
175+
if s < ans {
176+
ans = s
177+
}
178+
for _, nxt := range []string{op1(s, a), op2(s, b)} {
179+
if !vis[nxt] {
180+
vis[nxt] = true
181+
q = append(q, nxt)
182+
}
183+
}
184+
}
185+
return ans
186+
}
187+
188+
func op1(s string, a int) string {
189+
res := []byte(s)
190+
for i := 1; i < len(s); i += 2 {
191+
res[i] = byte((int(res[i]-'0')+a)%10 + '0')
192+
}
193+
return string(res)
194+
}
195+
196+
func op2(s string, b int) string {
197+
return s[len(s)-b:] + s[:len(s)-b]
198+
}
85199
```
86200

87201
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string findLexSmallestString(string s, int a, int b) {
4+
unordered_set<string> vis{{s}};
5+
queue<string> q{{s}};
6+
string ans = s;
7+
int n = s.size();
8+
while (!q.empty())
9+
{
10+
s = q.front();
11+
q.pop();
12+
if (s < ans) ans = s;
13+
string nxt1 = s;
14+
for (int i = 1; i < n; i += 2) nxt1[i] = ((nxt1[i] - '0' + a) % 10) + '0';
15+
string nxt2 = s.substr(n - b) + s.substr(0, n - b);
16+
for (string nxt : {nxt1, nxt2})
17+
{
18+
if (!vis.count(nxt))
19+
{
20+
vis.insert(nxt);
21+
q.push(nxt);
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func findLexSmallestString(s string, a int, b int) string {
2+
q := []string{s}
3+
vis := map[string]bool{s: true}
4+
ans := s
5+
for len(q) > 0 {
6+
s = q[0]
7+
q = q[1:]
8+
if s < ans {
9+
ans = s
10+
}
11+
for _, nxt := range []string{op1(s, a), op2(s, b)} {
12+
if !vis[nxt] {
13+
vis[nxt] = true
14+
q = append(q, nxt)
15+
}
16+
}
17+
}
18+
return ans
19+
}
20+
21+
func op1(s string, a int) string {
22+
res := []byte(s)
23+
for i := 1; i < len(s); i += 2 {
24+
res[i] = byte((int(res[i]-'0')+a)%10 + '0')
25+
}
26+
return string(res)
27+
}
28+
29+
func op2(s string, b int) string {
30+
return s[len(s)-b:] + s[:len(s)-b]
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String findLexSmallestString(String s, int a, int b) {
3+
Queue<String> q = new ArrayDeque<>();
4+
q.offer(s);
5+
Set<String> vis = new HashSet<>();
6+
vis.add(s);
7+
String ans = s;
8+
while (!q.isEmpty()) {
9+
s = q.poll();
10+
if (s.compareTo(ans) < 0) {
11+
ans = s;
12+
}
13+
char[] cs = s.toCharArray();
14+
for (int i = 1; i < cs.length; i += 2) {
15+
cs[i] = (char) (((cs[i] - '0' + a) % 10) + '0');
16+
}
17+
String nxt1 = String.valueOf(cs);
18+
String nxt2 = s.substring(b) + s.substring(0, b);
19+
for (String nxt : new String[]{nxt1, nxt2}) {
20+
if (!vis.contains(nxt)) {
21+
vis.add(nxt);
22+
q.offer(nxt);
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
}
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 s < ans:
9+
ans = s
10+
nxt1 = ''.join(
11+
[str((int(c) + a) % 10) if i & 1 else c for i, c in enumerate(s)]
12+
)
13+
nxt2 = s[-b:] + s[:-b]
14+
for nxt in (nxt1, nxt2):
15+
if nxt not in vis:
16+
vis.add(nxt)
17+
q.append(nxt)
18+
return ans

0 commit comments

Comments
 (0)