Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c4794d9

Browse filesBrowse files
authored
Merge pull request #104 from qilingit/day17
Add java solution2 for day16: Find All Anagrams in a String
2 parents 44514a8 + 87e3eee commit c4794d9
Copy full SHA for c4794d9

File tree

Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed

‎May-LeetCoding-Challenge/17-Find-All-Anagrams-In-A-String/Find-All-Anagrams-In-A-String.java

Copy file name to clipboardExpand all lines: May-LeetCoding-Challenge/17-Find-All-Anagrams-In-A-String/Find-All-Anagrams-In-A-String.java
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,36 @@ public boolean moveAndCompare(char[] str, int[] strCount, int toRemove, int toAd
3737
strCount[toAddIdx] += 1;
3838
return Arrays.equals(strCount, pCount);
3939
}
40+
}
41+
42+
class Solution2 {
43+
boolean isAnagram (String a, String b) {
44+
int[] arrayA = new int[26];
45+
int[] arrayB = new int[26];
46+
47+
for (int i = 0; i < a.length(); i++) {
48+
arrayA[a.charAt(i) - 'a']++;
49+
}
50+
51+
for (int j = 0; j < b.length(); j++) {
52+
arrayB[b.charAt(j) - 'a']++;
53+
}
54+
55+
for (int k = 0; k < 26; k++) {
56+
if(arrayA[k] != arrayB[k]) return false;
57+
}
58+
return true;
59+
}
60+
61+
public List<Integer> findAnagrams(String s, String p) {
62+
List<Integer> res = new ArrayList<>();
63+
//List<String> listSubString = new ArrayList<>();
64+
String subString = null;
65+
for (int i = 0; i < s.length() - p.length() + 1; i++) {
66+
subString = s.substring(i, p.length() + i);
67+
if(isAnagram(subString, p)) res.add(i);
68+
}
69+
70+
return res;
71+
}
4072
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.