Skip to content

Commit b0a08e5

Browse files
[N-0] refactor 567
1 parent 28e8e67 commit b0a08e5

File tree

3 files changed

+32
-47
lines changed

3 files changed

+32
-47
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Your ideas/fixes/algorithms are more than welcome!
123123
|573|[Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_573.java) | O(n) |O(1) | Medium | Math
124124
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_572.java) | O(m*n) |O(1) | Easy | Tree
125125
|568|[Maximum Vacation Days](https://leetcode.com/problems/maximum-vacation-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_568.java) | O(n^2*k) |O(n*k) | Hard | DP
126-
|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_567.java) | O(max(m,n)) |O(1) | Medium | Sliding Windows, Two Pointers
126+
|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_567.java) | O(l1 + 26*(l2 - l1)) |O(1) | Medium | Sliding Windows, Two Pointers
127127
|566|[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_566.java) | O(m*n) |O(1) | Easy |
128128
|565|[Array Nesting](https://leetcode.com/problems/array-nesting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_565.java) | O(n) |O(n) | Medium |
129129
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_563.java) | O(n) |O(n) | Easy | Tree Recursion

src/main/java/com/fishercoder/solutions/_567.java

+31-43
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,45 @@
2121
*/
2222
public class _567 {
2323

24-
//credit: sliding window: https://discuss.leetcode.com/topic/87845/java-solution-sliding-window
25-
/**1. How do we know string p is a permutation of string s? Easy, each character in p is in s too.
26-
* So we can abstract all permutation strings of s to a map (Character -> Count). i.e. abba -> {a:2, b:2}.
27-
* Since there are only 26 lower case letters in this problem, we can just use an array to represent the map.
28-
29-
2. How do we know string s2 contains a permutation of s1?
30-
We just need to create a sliding window with length of s1,
31-
move from beginning to the end of s2.
32-
When a character moves in from right of the window,
33-
we subtract 1 to that character count from the map.
34-
When a character moves out from left of the window,
35-
we add 1 to that character count. So once we see all zeros in the map,
36-
meaning equal numbers of every characters between s1 and the substring in the sliding window, we know the answer is true.
37-
*/
38-
public boolean checkInclusion(String s1, String s2) {
39-
int len1 = s1.length();
40-
int len2 = s2.length();
41-
if (len1 > len2) {
42-
return false;
43-
}
44-
45-
int[] count = new int[26];
46-
for (int i = 0; i < len1; i++) {
47-
count[s1.charAt(i) - 'a']++;
48-
}
49-
50-
for (int i = 0; i < len1; i++) {
51-
count[s2.charAt(i) - 'a']--;
52-
}
24+
public static class Solution1 {
25+
/**
26+
* credit: sliding window: https://discuss.leetcode.com/topic/87845/java-solution-sliding-window
27+
*/
28+
public boolean checkInclusion(String s1, String s2) {
29+
int len1 = s1.length();
30+
int len2 = s2.length();
31+
if (len1 > len2) {
32+
return false;
33+
}
5334

54-
if (allZeroes(count)) {
55-
return true;
56-
}
35+
int[] count = new int[26];
36+
for (int i = 0; i < len1; i++) {
37+
count[s1.charAt(i) - 'a']++;
38+
count[s2.charAt(i) - 'a']--;
39+
}
5740

58-
for (int i = len1; i < len2; i++) {
59-
count[s2.charAt(i) - 'a']--;
60-
count[s2.charAt(i - len1) - 'a']++;
6141
if (allZeroes(count)) {
6242
return true;
6343
}
64-
}
6544

66-
return false;
67-
}
45+
for (int i = len1; i < len2; i++) {
46+
count[s2.charAt(i) - 'a']--;
47+
count[s2.charAt(i - len1) - 'a']++;
48+
if (allZeroes(count)) {
49+
return true;
50+
}
51+
}
6852

69-
private boolean allZeroes(int[] count) {
70-
for (int i : count) {
71-
if (i != 0) {
72-
return false;
53+
return false;
54+
}
55+
56+
private boolean allZeroes(int[] count) {
57+
for (int i : count) {
58+
if (i != 0) {
59+
return false;
60+
}
7361
}
62+
return true;
7463
}
75-
return true;
7664
}
7765
}

src/test/java/com/fishercoder/_567Test.java

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 4/30/17.
11-
*/
129
public class _567Test {
1310
private static _567 test;
1411
private static boolean expected;

0 commit comments

Comments
 (0)