-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check1swapequalString.java
45 lines (36 loc) · 1.2 KB
/
Check1swapequalString.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Check1swapequalString {
/*
Runtime: 1 ms, faster than 100.00% of Java online submissions for Check if One String Swap Can Make Strings Equal.
Memory Usage: 37 MB, less than 100.00% of Java online submissions for Check if One String Swap Can Make Strings Equal.
*/
public boolean areAlmostEqual(String s1, String s2) {
if(s1==null||s2==null) return false;
if(s1.equals(s2)) return true;
int count=0; ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)!=s2.charAt(i)){
list.add(i);count++;
}
}
if(count==2)
{
if(s1.equals(swapChar(s2,list.get(0),list.get(1))))
{
return true;
}
else
{
return false;
}
}
return false;
}
public String swapChar(String s,int i,int j)
{
StringBuilder myString = new StringBuilder(s);
myString.setCharAt(i, s.charAt(j));
myString.setCharAt(j, s.charAt(i));
return myString.toString();
}
}