Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c03eb84

Browse files
add 1061
1 parent 23cff7e commit c03eb84

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ _If you like this project, please leave me a star._ ★
563563
| 1066 |[Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1066.java) | | Medium |Backtracking, DP|
564564
| 1065 |[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | | Medium ||
565565
| 1062 |[Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1062.java) | | Medium |String, Binary Search, DP, Rolling Hash, Suffix Array, Hash Function|
566+
| 1061 |[Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1061.java) | | Medium ||Union Find
566567
| 1057 |[Campus Bikes](https://leetcode.com/problems/campus-bikes/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1057.java) | | Medium ||Greedy, Sort
567568
| 1056 |[Confusing Number](https://leetcode.com/problems/confusing-number/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | | Easy ||
568569
| 1055 |[Fixed Point](https://leetcode.com/problems/fixed-point/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_1055.java) | | Easy ||
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1061 {
4+
public static class Solution1 {
5+
public String smallestEquivalentString(String s1, String s2, String baseStr) {
6+
UnionFind unionFind = new UnionFind();
7+
for (int i = 0; i < s1.length(); i++) {
8+
unionFind.union(s1.charAt(i), s2.charAt(i));
9+
}
10+
StringBuilder sb = new StringBuilder();
11+
for (char c : baseStr.toCharArray()) {
12+
sb.append((char) (unionFind.find(c) + 'a'));
13+
}
14+
return sb.toString();
15+
}
16+
17+
class UnionFind {
18+
int[] ids;
19+
20+
public UnionFind() {
21+
this.ids = new int[26];
22+
for (int i = 0; i < ids.length; i++) {
23+
ids[i] = i;
24+
}
25+
}
26+
27+
public void union(char a, char b) {
28+
int x = find(a);
29+
int y = find(b);
30+
if (x < y) {
31+
ids[y] = x;
32+
} else {
33+
ids[x] = y;
34+
}
35+
}
36+
37+
public int find(char x) {
38+
while (x - 'a' != ids[x - 'a']) {
39+
ids[x - 'a'] = ids[ids[x - 'a']];
40+
x = (char) (ids[x - 'a'] + 'a');
41+
}
42+
return x - 'a';
43+
}
44+
}
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1061;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1061Test {
10+
private static _1061.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1061.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("makkek", solution1.smallestEquivalentString("parker", "morris", "parser"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)