Skip to content

Commit c870b22

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 41.60 MB (Top 23.12%)
1 parent b492326 commit c870b22

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 41.60 MB (Top 23.12%)
2+
13
class Solution {
24
public String[] findOcurrences(String text, String first, String second) {
3-
List<String> list = new ArrayList<>();
4-
5-
String[] arr = text.split(" ");
6-
7-
for(int i = 0; i < arr.length - 2; i++) {
8-
if(arr[i].equals(first) && arr[i + 1].equals(second)) {
9-
list.add(arr[i + 2]);
10-
}
5+
String[] st = text.split(" ");
6+
List<String> l = new ArrayList<String>();
7+
int i =0,n = st.length;
8+
9+
while(i<n)
10+
{
11+
if(st[i].equals(first))
12+
if(i+1<n-1 && st[i+1].equals(second))
13+
l.add(st[i+2]);
14+
i++;
1115
}
12-
13-
String[] result = list.toArray(new String[0]);
14-
15-
return result;
16+
17+
return l.toArray(new String[0]);
1618
}
1719
}
20+

0 commit comments

Comments
 (0)