1078. Occurrences After Bigram #
题目 #
- 给出第一个词
first和第二个词second,考虑在某些文本text中可能以"first second third"形式出现的情况,其中second紧随first出现,third紧随second出现。 - 对于每种这样的情况,将第三个词 “
third” 添加到答案中,并返回答案。
思路 #
模拟 #
代码 #
模拟 #
class Solution {
public String[] findOcurrences(String text, String first, String second) {
List<String> ans = new LinkedList<>();
String[] words = text.split(" ");
for (int i = 0; i < words.length - 2; i++) {
if (words[i].equals(first) && words[i+1].equals(second)) ans.add(words[i+2]);
}
return ans.toArray(new String[ans.size()]);
}
}