20211226 力扣刷题 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> list = new ArrayList();
        String[] strs = text.split(" ");

        for(int i=0;i<strs.length-2;i++){
                if(strs[i].equals(first)&&strs[i+1].equals(second)){
                    list.add(strs[i+2]);
                }
        }

        String[] strArray = new String[list.size()];
        return list.toArray(strArray);
    }
}

 

执行结果:
通过

添加备注

执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:36.6 MB, 在所有 Java 提交中击败了29.00%的用户
通过测试用例:30 / 30

 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/occurrences-after-bigram
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

posted @ 2021-12-26 10:00  skystrivegao  阅读(36)  评论(0)    收藏  举报