-
Notifications
You must be signed in to change notification settings - Fork 1
/
Find All Anagrams in a String.java
66 lines (54 loc) · 1.76 KB
/
Find All Anagrams in a String.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
*Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
*Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
*The order of output does not matter.
**/
//Solution 1 Passes all cases
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> rst = new ArrayList<>();
if (s == null || s.length() == 0 || s.length() < p.length()) {
return rst;
}
int[] map_p = new int[26];
for (int i = 0; i < p.length(); i++) {
map_p[p.charAt(i) - 'a']++;
}
for (int i = 0; i <=s.length() - p.length(); i++) {
int[] map_s = new int[26];
for (int j = 0; j < p.length(); j++) {
map_s[s.charAt(i+j) - 'a']++;
}
if (isMatch(map_p, map_s)) {
rst.add(i);
}
}
return rst;
}
public boolean isMatch(int[] arr1, int[] arr2) {
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}
//Solution 2 Passes 35/36 Cases
class Solution {
public List<Integer> findAnagrams(String s, String p) {
char[] ch = p.toCharArray();
Arrays.sort(ch);
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<=s.length()-ch.length;i++)
{
char ch1[]=s.substring(i,i+ch.length).toCharArray();
Arrays.sort(ch1);
String str = new String(ch);
String str1 = new String(ch1);
if(str.equals(str1))
list.add(i);
}
return list;
}
}