10000 add 567 · bloomer1/Leetcode@9ebe7b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ebe7b8

Browse files
add 567
1 parent 3c97867 commit 9ebe7b8

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/stevesun/solutions/_567.java) | O(max(m,n)) |O(1) | Medium | Sliding Windows, Two Pointers
2324
|566|[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/)|[Solution](../master/src/main/java/com/stevesun/solutions/_566.java) | O(m*n) |O(1) | Easy |
2425
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)|[Solution](../master/src/main/java/com/stevesun/solutions/_563.java) | O(n) |O(n) | Easy | Tree Recursion
2526
|562|[Longest Line of Consecutive One in Matrix](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/)|[Solution](../master/src/main/java/com/stevesun/solutions/_562.java) | O(m*n) |O(m*n) | Medium | Matrix DP
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1.
5+
* In other words, one of the first string's permutations is the substring of the second string.
6+
7+
Example 1:
8+
Input:s1 = "ab" s2 = "eidbaooo"
9+
Output:True
10+
Explanation: s2 contains one permutation of s1 ("ba").
11+
12+
Example 2:
13+
Input:s1= "ab" s2 = "eidboaoo"
14+
Output: False
15+
16+
Note:
17+
The input strings only contain lower case letters.
18+
The length of both given strings is in range [1, 10,000].
19+
*/
20+
public class _567 {
21+
22+
//credit: sliding window: https://discuss.leetcode.com/topic/87845/java-solution-sliding-window
23+
public boolean checkInclusion(String s1, String s2) {
24+
int len1 = s1.length();
25+
int len2 = s2.length();
26+
if (len1 > len2) return false;
27+
28+
int[] count = new int[26];
29+
for (int i = 0; i < len1; i++) {
30+
count[s1.charAt(i) - 'a']++;
31+
}
32+
33+
for (int i = 0; i < len1; i++) {
34+
count[s2.charAt(i) - 'a']--;
35+
}
36+
37+
if (allZeroes(count)) return true;
38+
39+
for (int i = len1; i < len2; i++) {
40+
count[s2.charAt(i) - 'a']--;
41+
count[s2.charAt(i - len1) - 'a']++;
42+
if (allZeroes(count)) return true;
43+
}
44+
45+
return false;
46+
}
47+
48+
private boolean allZeroes(int[] count) {
49+
for (int i : count) {
50+
if (i != 0) return false;
51+
}
52+
return true;
53+
}
54+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._567;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 4/30/17.
11+
*/
12+
public class _567Test {
13+
private static _567 test;
14+
private static boolean expected;
15+
private static boolean actual;
16+
private static String s1;
17+
private static String s2;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new _567();
22+
}
23+
24+
@Test
25+
public void test1(){
26+
s1 = "ab";
27+
s2 = "eidbaooo";
28+
expected = true;
29+
actual = test.checkInclusion(s1, s2);
30+
assertEquals(expected, actual);
31+
}
32+
}

0 commit comments

Comments
 (0)
0