Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08cb786

Browse files
committedDec 6, 2021
solves occurrences afte bigram
1 parent 57d93d5 commit 08cb786

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed
 

‎README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@
281281
| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling) | | |
282282
| 1030 | [Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order) | [![Java](assets/java.png)](src/MatrixCellsInDistanceOrder.java) | |
283283
| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive) | | |
284-
| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang) | [![Java](assets/java.png)](src/ValidBoomerang.java) | |
284+
| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang) | [![Java](assets/java.png)](src/ValidBoomerang.java) | |
285285
| 1042 | [Flower Planting with no Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent) | | |
286-
| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight) | [![Java](assets/java.png)](src/LastStoneWeight.java) | |
287-
| 1047 | [Remove All adjacent Duplicates in String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string) | [![Java](assets/java.png)](src/RemoveAllAdjacentDuplicatesInAString.java) | |
286+
| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight) | [![Java](assets/java.png)](src/LastStoneWeight.java) | |
287+
| 1047 | [Remove All adjacent Duplicates in String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string) | [![Java](assets/java.png)](src/RemoveAllAdjacentDuplicatesInAString.java) | |
288288
| 1051 | [Height Checker](https://leetcode.com/problems/height-checker) | | |
289-
| 1056 | 🔒 [Confusing Number](https://leetcode.com/problems/confusing-number) | | |
290-
| 1064 | 🔒 [Fixed Point](https://leetcode.com/problems/fixed-point) | | |
291-
| 1065 | 🔒 [Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string) | | |
292-
| 1071 | [Greatest Common Divisors of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings) | [![Java](assets/java.png)](src/GreatestCommonDivisorOfStrings.java) | |
293-
| 1078 | [Occurrence After Bigram](https://leetcode.com/problems/occurrences-after-bigram) | | |
289+
| 1056 | 🔒 [Confusing Number](https://leetcode.com/problems/confusing-number) | | |
290+
| 1064 | 🔒 [Fixed Point](https://leetcode.com/problems/fixed-point) | | |
291+
| 1065 | 🔒 [Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string) | | |
292+
| 1071 | [Greatest Common Divisors of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings) | [![Java](assets/java.png)](src/GreatestCommonDivisorOfStrings.java) | |
293+
| 1078 | [Occurrence After Bigram](https://leetcode.com/problems/occurrences-after-bigram) | [![Java](assets/java.png)](src/OccurrencesAfterBigram.java) | |
294294
| 1085 | [Sum of Digits in Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number) | | |
295295
| 1086 | [High Five](https://leetcode.com/problems/high-five) | | |
296296
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros) | | |

‎src/OccurrencesAfterBigram.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class OccurrencesAfterBigram {
5+
public String[] findOcurrences(String text, String first, String second) {
6+
List<String> result = new ArrayList<>();
7+
for (int fromIndex = 0 ; fromIndex < text.length() ; fromIndex += first.length() + 1) {
8+
fromIndex = text.indexOf(first, fromIndex);
9+
if (fromIndex == -1) break;
10+
if (areEqual(text, fromIndex + first.length() + 1, second)
11+
&& fromIndex + first.length() + second.length() + 2 < text.length()) {
12+
result.add(nextWord(text, fromIndex + first.length() + second.length() + 2));
13+
}
14+
}
15+
return result.toArray(new String[0]);
16+
}
17+
18+
private boolean areEqual(String text, int fromIndex, String pattern) {
19+
if (pattern.length() > text.length() - fromIndex) return false;
20+
for (int index = fromIndex ; index < text.length() && index - fromIndex < pattern.length() ; index++) {
21+
if (text.charAt(index) != pattern.charAt(index - fromIndex)) return false;
22+
}
23+
return true;
24+
}
25+
26+
private String nextWord(String text, int startIndex) {
27+
StringBuilder result = new StringBuilder();
28+
for (int index = startIndex ; index < text.length() && text.charAt(index) != ' ' ; index++) {
29+
result.append(text.charAt(index));
30+
}
31+
return result.toString();
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.