-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p5.java
More file actions
60 lines (55 loc) · 1.64 KB
/
Copy pathq1p5.java
File metadata and controls
60 lines (55 loc) · 1.64 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.lang.*;
public class q1p5{
public static void main(String[] args){
String test1 = "bae";
String test2 = "pale";
System.out.println(check1(test1,test2));
}
// T: O(n) n is the shorter string's length
// S: O(1)
//
public static boolean check1(String str1, String str2){
int len1 = str1.length();
int len2 = str2.length();
if(Math.abs(len1-len2)>=2)
return false;
if(len1 == len2){
return checkEqual(str1, str2);
}else
return checkUnequal(str1, str2);
}
public static boolean checkEqual(String str1, String str2){
int len = str1.length();
boolean flag = false;
for(int i = 0; i<len; i++){
if(str1.charAt(i) != str2.charAt(i) && flag == true)
return false;
if(str1.charAt(i) != str2.charAt(i))
flag = true;
}
return true;
}
public static boolean checkUnequal(String str1, String str2){
String longStr;
String shortStr;
if(str1.length() > str2.length()){
longStr = str1;
shortStr = str2;
}else
longStr = str2;
shortStr = str1;
boolean flag = false;
int len = shortStr.length();
for(int i = 0; i<len; i++){
if(flag == true && shortStr.charAt(i) != longStr.charAt(i+1)){
return false;
}
if(flag == false && shortStr.charAt(i) != longStr.charAt(i)){
flag = true;
i--;
}
}
return true;
}
}