Skip to content

Commit 9ff75b2

Browse files
committedJul 14, 2023
0567-permutation-in-string_UPDATED
much simpler solution with the same frequency and sliding window idea.
1 parent 723a252 commit 9ff75b2

8 files changed

+111
-30
lines changed
 

‎.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/codeStyles/Project.xml

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Neetcode-update.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

‎java/0567-permutation-in-string.java

+16-30
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
class Solution {
2-
1+
public class Solution {
32
public boolean checkInclusion(String s1, String s2) {
4-
if (s2.length() < s1.length()) return false;
5-
int[] arr = new int[26];
6-
//add the values to the hash array
7-
for (int i = 0; i < s1.length(); i++) {
8-
arr[s1.charAt(i) - 'a']++;
3+
int n = s1.length();
4+
int[] freq = new int[26];
5+
int m = s2.length();
6+
for (int i = 0; i < n; i++) {
7+
freq[s1.charAt(i) - 'a']++;
98
}
10-
int i = 0;
11-
int j = 0;
12-
//point j to it's position
13-
for (; j < s1.length(); j++) {
14-
arr[s2.charAt(j) - 'a']--;
9+
int[] freq2 = new int[26];
10+
for (int i = 0; i < m; i++) {
11+
freq2[s2.charAt(i) - 'a']++;
12+
if (i >= n) {
13+
freq2[s2.charAt(i - n) - 'a']--;
14+
}
15+
if (Arrays.equals(freq, freq2))
16+
return true;
1517
}
16-
j--;
17-
if (isEmpty(arr)) return true;
18-
while (j < s2.length()) {
19-
arr[s2.charAt(i) - 'a']++;
20-
i++;
21-
j++;
22-
if (j < s2.length()) arr[s2.charAt(j) - 'a']--;
23-
if (isEmpty(arr)) return true;
24-
}
25-
return isEmpty(arr);
26-
}
27-
28-
public boolean isEmpty(int[] arr) {
29-
for (int i = 0; i < arr.length; i++) {
30-
if (arr[i] != 0) return false;
31-
}
32-
return true;
18+
return false;
3319
}
34-
}
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.