Skip to content

Commit 48d8023

Browse files
committed
Add solution for 1023
1 parent 0c191fe commit 48d8023

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

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

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1023|[Camelcase Matching](https://leetcode.com/problems/camelcase-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1023.java)| O(mn) | O(n) | |Medium| String |
3031
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | O(n) | O(n) | |Easy|
3132
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3233
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
* A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it
8+
* equals the query. (We may insert each character at any position, and may insert 0 characters.)
9+
*
10+
* Given a list of queries, and a pattern, return an answer list of booleans,
11+
* where answer[i] is true if and only if queries[i] matches the pattern.
12+
*
13+
* Example:
14+
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
15+
* Output: [true,false,true,true,false]
16+
*
17+
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
18+
* Output: [false,true,false,false,false]
19+
*/
20+
21+
public class _1023 {
22+
23+
public static class Solution1 {
24+
public List<Boolean> camelMatch(String[] queries, String pattern) {
25+
List<Boolean> res = new ArrayList<>();
26+
27+
for (String s : queries) {
28+
res.add(match(s, pattern));
29+
}
30+
31+
return res;
32+
}
33+
34+
public boolean match(String s, String pattern) {
35+
char[] st = s.toCharArray();
36+
37+
int i = 0;
38+
for (char c : pattern.toCharArray()) {
39+
while (i < st.length && st[i] != c) {
40+
if (Character.isUpperCase(st[i])) return false;
41+
++i;
42+
}
43+
44+
if (i == st.length) return false;
45+
++i;
46+
}
47+
48+
while (i < st.length) {
49+
if (Character.isUpperCase(st[i])) return false;
50+
++i;
51+
}
52+
53+
return true;
54+
}
55+
}
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1023;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1023Test {
13+
private static _1023.Solution1 solution1;
14+
private String[] input = {"FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"};
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1023.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
String pattern = "FB";
24+
List<Boolean> expected = Arrays.asList(true, false, true, true, false);
25+
assertEquals(solution1.camelMatch(input, pattern), expected);
26+
}
27+
28+
@Test
29+
public void test2() {
30+
String pattern = "FoBa";
31+
List<Boolean> expected = Arrays.asList(true, false, true, false, false);
32+
assertEquals(solution1.camelMatch(input, pattern), expected);
33+
}
34+
35+
@Test
36+
public void test3() {
37+
String pattern = "FoBaT";
38+
List<Boolean> expected = Arrays.asList(false, true, false, false, false);
39+
assertEquals(solution1.camelMatch(input, pattern), expected);
40+
}
41+
}

0 commit comments

Comments
 (0)