Skip to content

Commit 0644f0c

Browse files
refactor 5083
1 parent 849f10b commit 0644f0c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.stream.Collectors;
4+
import java.util.stream.IntStream;
55

66
/**
77
* 5083. Occurrences After Bigram
8-
*
8+
* <p>
99
* Given words first and second, consider occurrences in some text of the form "first second third",
1010
* where second comes immediately after first, and third comes immediately after second.
1111
* For each such occurrence, add "third" to the answer, and return the answer.
12-
*
12+
* <p>
1313
* Example 1:
1414
* Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
1515
* Output: ["girl","student"]
16-
*
16+
* <p>
1717
* Example 2:
1818
* Input: text = "we will we will rock you", first = "we", second = "will"
1919
* Output: ["we","rock"]
20-
*
20+
* <p>
2121
* Note:
2222
* 1 <= text.length <= 1000
2323
* text consists of space separated words, where each word consists of lowercase English letters.
2424
* 1 <= first.length, second.length <= 10
2525
* first and second consist of lowercase English letters.
26-
* */
26+
*/
2727
public class _5083 {
2828
public static class Solution1 {
2929
public String[] findOcurrences(String text, String first, String second) {
30-
List<String> result = new ArrayList<>();
3130
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;
31+
return IntStream
32+
.range(0, words.length - 2)
33+
.filter(i -> words[i].equals(first) && words[i + 1].equals(second))
34+
.mapToObj(i -> words[i + 2])
35+
.collect(Collectors.toList())
36+
.stream()
37+
.toArray(String[]::new);
4238
}
4339
}
4440
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy