Skip to content

Commit 4ee26dc

Browse files
add 1790
1 parent 8154859 commit 4ee26dc

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1791|[Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1791.java) ||Medium|Graph|
12+
|1790|[Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1790.java) ||Easy|String|
1213
|1785|[Minimum Elements to Add to Form a Given Sum](https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1785.java) ||Medium|Greedy|
1314
|1784|[Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1784.java) ||Easy|Greedy|
1415
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1781.java) ||Medium|HashTable, String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1790 {
4+
public static class Solution1 {
5+
public boolean areAlmostEqual(String s1, String s2) {
6+
if (s1.equals(s2)) {
7+
return true;
8+
}
9+
for (int i = 0; i < s1.length(); i++) {
10+
for (int j = 0; j < i; j++) {
11+
String newS1 = swap(s1, i, j);
12+
if (newS1.equals(s2)) {
13+
return true;
14+
}
15+
}
16+
}
17+
return false;
18+
}
19+
20+
private String swap(String str, int i, int j) {
21+
char c = str.charAt(i);
22+
StringBuilder sb = new StringBuilder(str);
23+
sb.replace(i, i + 1, str.charAt(j) + "");
24+
sb.replace(j, j + 1, "" + c);
25+
return sb.toString();
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1790;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1790Test {
10+
private static _1790.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1790.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(false, solution1.areAlmostEqual("a", "z"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.areAlmostEqual("bank", "kanb"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)