-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAreAlmostEqual.java
More file actions
25 lines (23 loc) · 914 Bytes
/
Copy pathAreAlmostEqual.java
File metadata and controls
25 lines (23 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package LeetcodePractice;
public class AreAlmostEqual {
public boolean areAlmostEqual(String s1, String s2) {
if (s1.equals(s2)) return true;
int n = s1.length();
int first = -1, second = -1, diff = 0;
for (int i = 0; i < n; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
diff++;
if (first == -1) first = i;
else if (second == -1) second = i;
else return false;
}
}
return diff == 2 && s1.charAt(first) == s2.charAt(second) && s1.charAt(second) == s2.charAt(first);
}
public static void main(String[] args) {
AreAlmostEqual sol = new AreAlmostEqual();
System.out.println(sol.areAlmostEqual("bank", "kanb"));
System.out.println(sol.areAlmostEqual("attack", "defend"));
System.out.println(sol.areAlmostEqual("kelb", "kelb"));
}
}