给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 "first second third" 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。
对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。
示例 1:
输入:text = "alice is a good girl she is a good student", first = "a", second = "good"
输出:["girl","student"]
链接:https://leetcode-cn.com/problems/occurrences-after-bigram
思路:分隔,遍历
class Solution { public String[] findOcurrences(String text, String first, String second) { String[] split = text.split("\\s"); //System.out.println(split.length); ArrayList<String> array = new ArrayList<>(); for(int i=1;i<split.length-1;i++) { if(split[i-1].equals(first) && split[i].equals(second) ) { array.add(split[i+1]); //System.out.println(split[i+1]); } } return (String [])array.toArray(new String[array.size()]); } }
原文:https://www.cnblogs.com/cocobear9/p/12883461.html