Skip to content

Commit 54c3bc2

Browse files
committed
Add solution for 1023
1 parent 1785d5e commit 54c3bc2

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Your ideas/fixes/algorithms are more than welcome!
2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy|
31+
|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 |
3132
|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|
3233
|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|
3334
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | O(mn) | O(mn) | |Medium|Graph, DFS, BFS, recursion|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1023. Camelcase Matching
8+
*
9+
* A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it
10+
* equals the query. (We may insert each character at any position, and may insert 0 characters.)
11+
*
12+
* Given a list of queries, and a pattern, return an answer list of booleans,
13+
* where answer[i] is true if and only if queries[i] matches the pattern.
14+
*
15+
* Example:
16+
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
17+
* Output: [true,false,true,true,false]
18+
*
19+
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
20+
* Output: [false,true,false,false,false]
21+
*/
22+
23+
public class _1023 {
24+
25+
public static class Solution1 {
26+
public List<Boolean> camelMatch(String[] queries, String pattern) {
27+
List<Boolean> res = new ArrayList<>();
28+
29+
for (String s : queries) {
30+
res.add(match(s, pattern));
31+
}
32+
33+
return res;
34+
}
35+
36+
public boolean match(String s, String pattern) {
37+
char[] st = s.toCharArray();
38+
39+
int i = 0;
40+
for (char c : pattern.toCharArray()) {
41+
while (i < st.length && st[i] != c) {
42+
if (Character.isUpperCase(st[i])) {
43+
return false;
44+
}
45+
++i;
46+
}
47+
48+
if (i == st.length) {
49+
return false;
50+
}
51+
++i;
52+
}
53+
54+
while (i < st.length) {
55+
if (Character.isUpperCase(st[i])) {
56+
return false;
57+
}
58+
++i;
59+
}
60+
61+
return true;
62+
}
63+
}
64+
}
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)