-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathAnagramsInString.java
143 lines (124 loc) · 4.27 KB
/
AnagramsInString.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.leetcode.strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Level: Medium
* Problem: https://leetcode.com/problems/find-all-anagrams-in-a-string/
* Description:
* 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.
*
* Example 1:
*
* Input:
* s: "cbaebabacd" p: "abc"
*
* Output:
* [0, 6]
*
* Explanation:
* The substring with start index = 0 is "cba", which is an anagram of "abc".
* The substring with start index = 6 is "bac", which is an anagram of "abc".
*
* Example 2:
*
* Input:
* s: "abab" p: "ab"
*
* Output:
* [0, 1, 2]
*
* Explanation:
* The substring with start index = 0 is "ab", which is an anagram of "ab".
* The substring with start index = 1 is "ba", which is an anagram of "ab".
* The substring with start index = 2 is "ab", which is an anagram of "ab".
*
* @author rampatra
* @since 2019-04-13
*/
public class AnagramsInString {
/**
* Time complexity: O((n-m) * m log m)
* where,
* n = text length
* m = pattern length
*
* @param text
* @param pattern
* @return
*/
private static List<Integer> findAllAnagramsInTextNaive(String text, String pattern) {
List<Integer> indexes = new ArrayList<>();
int textLen = text.length();
int patternLen = pattern.length();
char[] patternChars = pattern.toCharArray();
Arrays.sort(patternChars); // takes O(m log m) time
String patternSorted = String.valueOf(patternChars);
String subText;
char[] subTextChars;
String subTextSorted;
for (int i = 0; i <= textLen - patternLen; i++) { // loops n-m number of times
subText = text.substring(i, i + patternLen);
subTextChars = subText.toCharArray();
Arrays.sort(subTextChars); // sorts m number of characters, takes O(m log m)
subTextSorted = String.valueOf(subTextChars);
if (subTextSorted.equals(patternSorted)) { // compare m characters takes m time
indexes.add(i);
}
}
return indexes;
}
/**
* Time complexity: O(n)
* where,
* n = length of text or number of characters in text
* <p>
* Runtime: <a href="https://leetcode.com/submissions/detail/253261320/">6 ms</a>.
*
* @param text
* @param pattern
* @return
*/
private static List<Integer> findAllAnagramsInText(String text, String pattern) {
List<Integer> indices = new ArrayList<>();
int textLen = text.length();
int patternLen = pattern.length();
int[] textCharCountInWindow = new int[26];
int[] patternCharCount = new int[26];
for (int i = 0; i < patternLen; i++) {
patternCharCount[pattern.charAt(i) - 'a']++;
}
for (int i = 0; i < textLen; i++) {
textCharCountInWindow[text.charAt(i) - 'a']++;
if (i >= patternLen) {
textCharCountInWindow[text.charAt(i - patternLen) - 'a']--;
}
if (Arrays.equals(textCharCountInWindow, patternCharCount)) { // loops 26 times no matter the text/pattern length
indices.add(i - patternLen + 1);
}
}
return indices;
}
public static void main(String[] args) {
// basic use cases
System.out.println(findAllAnagramsInTextNaive("cbaebabacd", "abc"));
System.out.println(findAllAnagramsInTextNaive("abab", "ab"));
System.out.println(findAllAnagramsInTextNaive("af", "af"));
System.out.println(findAllAnagramsInTextNaive("af", "be"));
// corner case
System.out.println(findAllAnagramsInTextNaive("", "ab"));
System.out.println("-----");
// basic use cases
System.out.println(findAllAnagramsInText("cbaebabacd", "abc"));
System.out.println(findAllAnagramsInText("abab", "ab"));
System.out.println(findAllAnagramsInText("af", "af"));
System.out.println(findAllAnagramsInText("af", "be"));
// corner case
System.out.println(findAllAnagramsInText("", "ab"));
}
}