8000 add 5083 · ajinkyashendre/Leetcode@a307614 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit a307614

Browse files
add 5083
1 parent fd703c7 commit a307614

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-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+
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
3031
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3132
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
3233
|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | O(n) | O(1) | |Easy||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 5083. Occurrences After Bigram
8+
*
9+
* Given words first and second, consider occurrences in some text of the form "first second third",
10+
* where second comes immediately after first, and third comes immediately after second.
11+
* For each such occurrence, add "third" to the answer, and return the answer.
12+
*
13+
* Example 1:
14+
* Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
15+
* Output: ["girl","student"]
16+
*
17+
* Example 2:
18+
* Input: text = "we will we will rock you", first = "we", second = "will"
19+
* Output: ["we","rock"]
20+
*
21+
* Note:
22+
* 1 <= text.length <= 1000
23+
* text consists of space separated words, where each word consists of lowercase English letters.
24+
* 1 <= first.length, second.length <= 10
25+
* first and second consist of lowercase English letters.
26+
* */
27+
public class _5083 {
28+
public static class Solution1 {
29+
public String[] findOcurrences(String text, String first, String second) {
30+
List<String> result = new ArrayList<>();
31+
String[] words = text.split(" ");
32+
for (int i = 0; i < words.length - 2; i++) {
33+
if (words[i].equals(first) && words[i + 1].equals(second)) {
34+
result.add(words[i + 2]);
35+
}
36+
}
37+
String[] occ = new String[result.size()];
38+
for (int i = 0; i < result.size(); i++) {
39+
occ[i] = result.get(i);
40+
}
41+
return occ;
42+
}
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._5083;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _5083Test {
10+
private static _5083.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _5083.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new String[]{"girl", "student"}, solution1.findOcurrences("alice is a good girl she is a good student", "a", "good"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new String[]{"we", "rock"}, solution1.findOcurrences("we will we will rock you", "we", "will"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
0