JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • algorithm
  • leetcode
JavaInterview.cn
2022-10-15
目录

匹配子序列的单词数Java

文章发布较早,内容可能过时,阅读注意甄别。

# 题目

给定字符串 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 都只由小写字母组成。

# 思路

Java Map+Deque

思路即是将相同首字母的字符串放在一个队列中,

每次从s原字符串读入字符,并在words字符串中寻找是否有以该字符为首的字符串,

若有则将第二位字符作为该word字符串首字母添加到哈希表中继续迭代,

直到找到word的最后一个字符,说明匹配成功

Map结合队列

Map<Character,Deque<String>> m = new HashMap<>();

# 解法


class Solution {
    public int numMatchingSubseq(String s, String[] words) {
        // 哈希表加队列 即 key为26个字母 value则为word中那些string构成的队列
        Map<Character,Deque<String>> m = new HashMap<>();
        for(char c='a'; c<='z';c++){
            m.put(c,new LinkedList<>());
        }
        // 将相同首字母的字符串放在一个队列中
        for(String w : words){
            m.get(w.charAt(0)).add(w);
        }
        int cnt =0;
        for(char ch : s.toCharArray()){
            // 弹出对应首字母的队列 其中都是以这个字符为首字母的字符串
            Deque<String> q= m.get(ch);
            int sz = q.size();
            for(int i=0; i<sz;i++){
                String str = q.poll();
                if(str.length()==1) cnt++;
                else{
                    m.get(str.charAt(1)).offer(str.substring(1));
                }
            }
        }
        return cnt;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 总结

  • 分析出几种情况,然后分别对各个情况实现
微信 支付宝
最近更新
01
1686. 石子游戏VI Java
08-18
02
1688. 比赛中的配对次数 Java
08-18
03
1687. 从仓库到码头运输箱子 Java
08-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式