Skip to content

Commit 780d460

Browse files
solves check if string is prefix of array
1 parent 1834f65 commit 780d460

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@
469469
| 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences) | [![Java](assets/java.png)](src/CheckIfAllCharactersHaveEqualNumberOfOccurrences.java) | |
470470
| 1945 | [Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert) | [![Java](assets/java.png)](src/SumOfDigitsOfStringAfterConvert.java) | |
471471
| 1952 | [Three Divisors](https://leetcode.com/problems/three-divisors) | [![Java](assets/java.png)](src/ThreeDivisors.java) | |
472-
| 1957 | [Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string) | | |
472+
| 1957 | [Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string) | [![Java](assets/java.png)](src/CheckIfStringIsAPrefixOfArray.java) | |
473473
| 1961 | [Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array) | | |
474474
| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word) | | |
475475
| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) | | |
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/check-if-string-is-a-prefix-of-array
2+
// T : O(|s|)
3+
// S : O(1)
4+
5+
public class CheckIfStringIsAPrefixOfArray {
6+
public boolean isPrefixString(String s, String[] words) {
7+
int w = 0, j = 0;
8+
for (int i = 0 ; i < s.length() && w < words.length ; i++) {
9+
if (s.charAt(i) != words[w].charAt(j)) return false;
10+
if (j == words[w].length() - 1) {
11+
if (w == words.length - 1 && i + 1 < s.length()) return false;
12+
w++;
13+
j = 0;
14+
} else j++;
15+
}
16+
return j == 0;
17+
}
18+
}

0 commit comments

Comments
 (0)