|
21 | 21 | */
|
22 | 22 | public class _567 {
|
23 | 23 |
|
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 | + } |
53 | 34 |
|
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 | + } |
57 | 40 |
|
58 |
| - for (int i = len1; i < len2; i++) { |
59 |
| - count[s2.charAt(i) - 'a']--; |
60 |
| - count[s2.charAt(i - len1) - 'a']++; |
61 | 41 | if (allZeroes(count)) {
|
62 | 42 | return true;
|
63 | 43 | }
|
64 |
| - } |
65 | 44 |
|
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 | + } |
68 | 52 |
|
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 | + } |
73 | 61 | }
|
| 62 | + return true; |
74 | 63 | }
|
75 |
| - return true; |
76 | 64 | }
|
77 | 65 | }
|
0 commit comments