-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckOneSwap.java
More file actions
34 lines (34 loc) · 962 Bytes
/
CheckOneSwap.java
File metadata and controls
34 lines (34 loc) · 962 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
26
27
28
29
30
31
32
33
34
import java.util.*;
public class CheckOneSwap {
public static boolean checkOneSwap(String s1 , String s2){
if (s1.length() != s2.length()) {
return false;
}
if (s1.equals(s2)) {
return true;
}
ArrayList<Integer> al = new ArrayList<>();
int n = s1.length();
for (int i = 0; i < n; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
al.add(i);
}
}
if (al.size() != 2) {
return false;
}
int a = al.get(0);
int b = al.get(1);
if (s1.charAt(a) == s2.charAt(b) && s1.charAt(b) == s2.charAt(a)) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
System.out.println(checkOneSwap(s1, s2));
sc.close();
}
}