匹配子序列的单词数

题目

给定字符串 s 和字符串数组 words, 返回  words[i] 中是s的子序列的单词个数 。

字符串的 子序列 是从原始字符串中生成的新字符串,可以从中删去一些字符(可以是none),而不改变其余字符的相对顺序。

例如, “ace” 是 “abcde” 的子序列。

示例 1:

输入: s = "abcde", words = ["a","bb","acd","ace"]
输出: 3
解释: 有三个是 s 的子序列的单词: "a", "acd", "ace"。
Example 2:

输入: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
输出: 2

提示:

1 <= s.length <= 5 * 104
1 <= words.length <= 5000
1 <= words[i].length <= 50
words[i]和 s 都只由小写字母组成

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

解法一:双指针

TLE.....

    public static int numMatchingSubseq(String s, String[] words) {
        char[] chars = s.toCharArray();
        int rs = 0;
        for (String word:words
             ) {
            if(isSubseq(s,word))
            {
                rs++;
            }
        }
        return rs;
    }
    private static boolean isSubseq(String s , String word)
    {
        if(s.length()<word.length())
        {
            return false;
        }
        if(s.equals(word))
        {
            return true;
        }
        int indexS=0;
        int indexW=0;
        while (indexS<s.length() && indexW<word.length())
        {
            if(s.charAt(indexS)==word.charAt(indexW) && indexW==word.length()-1)
            {
                return true;
            }else if(s.charAt(indexS)==word.charAt(indexW))
            {
                indexS++;
                indexW++;
            }else if(s.charAt(indexS)!=word.charAt(indexW))
            {
                indexS++;
            }
        }
        return false;
    }

解法二:根据解法一没过的测试用例优化,并添加了hash缓存

    public static int numMatchingSubseq2(String s, String[] words) {
        char[] chars = s.toCharArray();
        int rs = 0;
        HashMap<String,Boolean> map= new HashMap<>();
        for (String word:words
        ) {
            if(map.containsKey(word))
            {
                if(map.get(word))
                {
                    rs++;
                }
                continue;
            }
            if(isSubseq2(s,word))
            {
                rs++;
                map.put(word,true);
                continue;
            }
            map.put(word,false);
        }
        return rs;
    }
    private static boolean isSubseq2(String s , String word)
    {
        int indexS=0;
        int indexW=0;
        while (indexS<s.length() && indexW<word.length())
        {
            indexS= getSIndex(s,word.charAt(indexW),indexS);
            if(indexS==-1)
            {
                return false;
            }
            if(s.charAt(indexS)==word.charAt(indexW) && indexW==word.length()-1)
            {
                return true;
            }else if(s.charAt(indexS)==word.charAt(indexW))
            {
                indexS++;
                indexW++;
            }else if(s.charAt(indexS)!=word.charAt(indexW))
            {
                indexS++;
            }
        }
        return false;
    }
    private static int getSIndex(String s,char c,int indexS)
    {
       return s.indexOf(String.valueOf(c),indexS);
    }
posted @ 2022-11-17 10:00  花茶冰糖  阅读(16)  评论(0编辑  收藏  举报