IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode 792 Number of Matching Subsequences (二分或队列) -> 正文阅读

[数据结构与算法]LeetCode 792 Number of Matching Subsequences (二分或队列)

Given a string?s?and an array of strings?words, return?the number of?words[i]?that is a subsequence of?s.

A?subsequence?of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

  • For example,?"ace"?is a subsequence of?"abcde".

Example 1:

Input: s = "abcde", words = ["a","bb","acd","ace"]
Output: 3
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".

Example 2:

Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
Output: 2

Constraints:

  • 1 <= s.length <= 5 * 104
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • s?and?words[i]?consist of only lowercase English letters.

题目链接:https://leetcode.com/problems/number-of-matching-subsequences/

题目大意:求words中有多少是字符串s的子序列

题目分析:枚举words中的字符串判断,可预处理s中每个字符的下标位置,判断时二分查找大于等于当前位置的相等字符的下标

80ms,时间击败61.73%

class Solution {
    
    private int find(int pos, List<Integer> list) {
        int n = list.size(), ans = -1;
        int l = 0, r = n - 1, mid = 0;
        while (l <= r) {
            mid = (l + r) >> 1;
            int val = list.get(mid);
            if (val > pos) {
                ans = val;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return ans;
    }
    
    private boolean ok(String s, List[] chars) {
        int curPos = -1;
        for (int i = 0; i < s.length(); i++) {
            int idx = s.charAt(i) - 'a';
            if (chars[idx] == null) {
                return false;
            }
            curPos = find(curPos, chars[idx]);
            if (curPos == -1) {
                return false;
            }
        }
        return true;
    }
    
    public int numMatchingSubseq(String s, String[] words) {
        List<Integer>[] chars = new List[26];
        char[] str = s.toCharArray();
        for (int i = 0; i < str.length; i++) {
            int idx = str[i] - 'a';
            if (chars[idx] == null) {
                chars[idx] = new ArrayList<Integer>(); 
            }
            chars[idx].add(i);
        }
        int ans = 0;
        for (int i = 0; i < words.length; i++) {
            if (ok(words[i], chars)) {
                ans++;
            }
        }
        return ans;
    }
}

给26个字母每个字母建一个队列,用这些队列来记录s和words中字符串的字符顺序,枚举字符串s的每个字符并取得对应队列,将队列记录的字符串下标后移,移到末尾则答案加1,很巧妙的数据结构的运用

28ms,时间击败99.20%

class Solution {
    
    private class Str {
        String s;
        int pos;
        Str(String s, int pos) {
            this.s = s;
            this.pos = pos;
        }
        
        boolean isEnd() {
            return this.pos == this.s.length() - 1;
        }
    }
    
    public int numMatchingSubseq(String s, String[] words) {
        int ans = 0;
        Queue<Str>[] qs = new LinkedList[26];
        for (int i = 0; i < 26; i++) {
            qs[i] = new LinkedList<>();
        }
        for (int i = 0; i < words.length; i++) {
            qs[words[i].charAt(0) - 'a'].add(new Str(words[i], 0));
        }
        for (char c : s.toCharArray()) {
            Queue<Str> q = qs[c - 'a'];
            int sz = q.size();
            for (int i = 0; i < sz; i++) {
                Str cur = q.poll();
                if (cur.isEnd()) {
                    ans++;
                } else {
                    cur.pos++;
                    qs[cur.s.charAt(cur.pos) - 'a'].add(cur);
                }
            }
        }
        return ans;
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章           查看所有文章
加:2021-07-05 20:27:23  更:2021-07-05 20:28:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/20 14:38:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码