-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p2.java
More file actions
50 lines (47 loc) · 1.45 KB
/
Copy pathq1p2.java
File metadata and controls
50 lines (47 loc) · 1.45 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
import java.util.*;
import java.lang.*;
public class q1p2{
public static void main(String[] args){
String test1 = "qwrrtyuiop";
String test2 = "poiuytrewq";
System.out.println(method1(test1, test2));
System.out.println(method2(test1, test2));
}
// T: O(n)
// S: O(n)
// A: sort way, assumes letters only.
// note: if assumes ASCII, know that ASCII from 0~127, it can be use
// as index of array.
public static boolean method1(String test1, String test2){
if(test1.length() != test2.length())
return false;
char[] arr1 = test1.toCharArray();
char[] arr2 = test2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
for(int i = 0; i< arr1.length; i++){
if(arr1[i] != arr2[i])
return false;
}
return true;
}
// T: O(n)
// S: O(n)
// A: count character, assumes there only letters.
public static boolean method2(String test1, String test2){
if(test1.length() != test2.length())
return false;
int len = test1.length();
int[] count1 = new int[26];
int[] count2 = new int[26];
for(int i = 0; i<len; i++){
count1[test1.charAt(i)-'a']++;
count2[test2.charAt(i)-'a']++;
}
for(int i = 0; i<26; i++){
if(count1[i] != count2[i])
return false;
}
return true;
}
}