Skip to content

Commit 379c73e

Browse files
add 1625
1 parent 73c3084 commit 379c73e

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1625.java) ||BFS, DFS|Medium|
1112
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1624.java) ||String|Easy|
1213
|1620|[Coordinate With Maximum Network Quality](https://leetcode.com/problems/coordinate-with-maximum-network-quality/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1620.java) ||Greedy|Medium|
1314
|1619|[Mean of Array After Removing Some Elements](https://leetcode.com/problems/mean-of-array-after-removing-some-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1620.java) ||Array|Easy|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Set;
7+
8+
public class _1625 {
9+
public static class Solution1 {
10+
public String findLexSmallestString(String s, int a, int b) {
11+
Queue<String> queue = new LinkedList<>();
12+
Set<String> seen = new HashSet<>();
13+
queue.offer(s);
14+
String smallest = s;
15+
while (!queue.isEmpty()) {
16+
String current = queue.poll();
17+
//add
18+
char[] c = current.toCharArray();
19+
for (int i = 1; i < c.length; i++) {
20+
if (i % 2 == 1) {
21+
c[i] = (char) (((Integer.parseInt(String.valueOf(c[i])) + a) % 10) + '0');
22+
}
23+
}
24+
String next = new String(c);
25+
if (smallest.compareTo(next) > 0) {
26+
smallest = next;
27+
}
28+
if (seen.add(next)) {
29+
queue.add(next);
30+
}
31+
//rotate
32+
next = next.substring(next.length() - b) + next.substring(0, next.length() - b);
33+
if (seen.add(next)) {
34+
queue.add(next);
35+
}
36+
if (smallest.compareTo(next) > 0) {
37+
smallest = next;
38+
}
39+
}
40+
return smallest;
41+
}
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1625;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1625Test {
10+
private static _1625.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1625.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("2050", solution1.findLexSmallestString("5525", 9, 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("24", solution1.findLexSmallestString("74", 5, 1));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("0011", solution1.findLexSmallestString("0011", 4, 2));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("00553311", solution1.findLexSmallestString("43987654", 7, 3));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)