LintCode 647: Find All Anagrams in a String (滑动窗口经典题!!!)

告别昂贵硬件!用Unity3D 2023 + ROS2 Humble搭建你的第一个SLAM仿真实验室(保姆级教程) 本文提供了一份详细的教程,介绍如何使用Unity3D 2023和ROS2 Humble搭建SLAM仿真实验室,无需昂贵硬件即可实现虚拟传感器模拟和算法测试。通过Docker容器配置、ROS2安装优化及Unity插件集成,开发者可以快速构建高精度的仿真环境,适用于自主导航、多传感器标定等场景。 阅读详情
  1. Find All Anagrams in a String
    Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 40,000.

The order of output does not matter.

Example
Given s = “cbaebabacd” p = “abc”

return [0, 6]

The substring with start index = 0 is “cba”, which is an anagram of “abc”.
The substring with start index = 6 is “bac”, which is an anagram of “abc”.

思路:
滑动窗口。但是并不容易写。
有两个思路,分别见解法2和解法3.

解法1:先预处理string p得到bitmap 表。然后滑动窗口每动一下复制bitmap表,如果有match, bitmap表中相应元素频率-1。
由于每次都要复制,效率非常低。

class Solution {
public:
    /**
     * @param s: a string
     * @param p: a string
     * @return: a list of index
     */
    vector<int> findAnagrams(string &s, string &p) {
        int lenS = s.size();
        int lenP = p.size();
        
        vector<int> result;
        
        if ((lenS == 0) || (lenP == 0) || (lenS < lenP)) return result;
        
        vector<int> bitmap(26, 0);
        
        for (int i = 0; i < lenP; ++i) {
            bitmap[p[i] - 'a'] += 1;
        }
        
        int lastPos = lenS - lenP + 1;
        
        for (int i = 0; i < lastPos; ++i) {
            int count = 0;
            vector<int> newbitmap(bitmap);    
            
            for (int j = 0; j < lenP; ++j) {
                if (newbitmap[s[i + j] - 'a'] > 0) { 
                    newbitmap[s[i + j] - 'a'] -= 1;
                    count++;                    
                } else {
                    break;
                }
            }
            if (count == lenP) result.push_back(i); 
        }
        
        return result;
    }
};

解法2:是在网上看到的。感觉非常不错。效率很高。

class Solution {
public:
    /**
     * @param s: a string
     * @param p: a string
     * @return: a list of index
     */
    vector<int> findAnagrams(string &s, string &p) {
        int lenS = s.size();
        int lenP = p.size();
        
        vector<int> result;
        
        if ((lenS == 0) || (lenP == 0) || (lenS < lenP)) return result;
        
        vector<int> table(26, 0);
        
        for (int i = 0; i < lenP; ++i) {
            table[p[i] - 'a'] += 1;
        }
        
        int start = 0, end = 0, count = 0;
        
        while(end < lenS) {
            int index = s[end] - 'a';
            if (table[index] > 0) {
                count++;
            }
            table[index]--;
            
            if (end - start + 1 == lenP) {
                if (count == lenP) {
                    result.push_back(start);
                }
                int origIndex = s[start] - 'a';
                if (table[origIndex] >= 0) {
                    count--;
                }
                table[origIndex]++;
                start++; end++;
            } else {
                end++;
            }
        }
        
        return result;
    }
};

解法3:同向双指针,思路见
https://blog.youkuaiyun.com/roufoo/article/details/127944794?spm=1001.2014.3001.5501

class Solution {
public:
    /**
     * @param s: a string
     * @param p: a string
     * @return: a list of index
     */
    vector<int> findAnagrams(string &s, string &p) {
        int lenS = s.size(), lenP = p.size();
        if (lenS < lenP) return {};
        
        vector<int> freq(26, 0);
        for (int i = 0; i < lenP; i++) {
            freq[p[i] - 'a']++;
        }
        int count = lenP, j = 0;
        vector<int> res;    
        for (int i = 0; i < lenS; i++) { //i is left border, j is right border of the window
            j = max(i, j);
            while (count > 0 && j < lenS) {
                if (freq[s[j] - 'a'] > 0) count--;                
                freq[s[j] - 'a']--;
                j++;
            }
            if (count == 0 && j - i == lenP) res.push_back(i);
            if (freq[s[i] - 'a'] == 0) count++;
            freq[s[i] - 'a']++;
        }
        return res;
    }
};

四刷: 还是同向双指针,不过是逐个移动右指针,后批量移动左指针。上面的做法是批量移动右指针,后逐个移动左指针。

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int lenS = s.size(), lenP = p.size(), needAccount = 0, validAccount = 0;
        vector<int> res;
        vector<int> need(128, 0), actual(128, 0);
        int p1 = 0, p2 = 0;
        for (int i= 0; i < lenP; i++) {
            if (need[p[i]] == 0) needAccount++;
            need[p[i]]++;
        }
        while (p2 < lenS) {
            char c = s[p2];
            if (need[c] > 0) {
                actual[c]++;
                if (actual[c] == need[c]) {
                    validAccount++;
                }
            }
            p2++;
            while (validAccount == needAccount) {
                if (p2 - p1 == lenP) res.push_back(p1);
                c = s[p1];
                if (need[c] > 0) {
                    if (actual[c] == need[c]) {
                        validAccount--;
                    }
                    actual[c]--;
                }
                p1++;
            }
        }
        return res;
    }   
};
Windows下Cppcheck静态测试全流程:从安装到生成HTML报告的避坑指南 本文详细介绍了在Windows平台下使用Cppcheck进行静态代码分析的全流程,包括环境搭建、插件配置、自动化脚本编写及HTML报告生成。重点解析了MISRA插件集成、常见问题排查以及性能优化技巧,帮助C/C++开发者高效提升代码质量。文章特别针对Windows环境下的路径处理、空格问题等细节提供了实用解决方案。 阅读详情

相关推荐

雷达原理习题答案【西电】

  目前教材用的是陈老师新版的,但是,我发现课后题有一部分是之前某本书里的。答案如下【建议参看原题→查阅答案】 【原题点这里!!点这里!!】 目录跳转功能目前好像只支持网页版 文章目录% 第一章、第二章% 第三章% 第四章% 第五章% 第六章% 第七章% 第八章 % 第一章、第二章 % 第三章 % 第四章 % 第五章 % 第六章 % 第七章 % 第八章 ...

薛定谔的壳的博客 1万+

Leetcode 438 Find All Anagrams in a String + Leetcode 567 Permutation in String(两题几乎一样)

给定两个字符串s和p,求s的子串所有的起点,满足这些子串是p的anagrams(字符的频率相同,但是可能顺序不同)定长滑动窗口,统计窗口内的字符是否是p的anagrams。空间复杂度:O(m) m为p的长度hu。时间复杂度:O(n) n 为s的长度。

xxxmmc的博客 281

ST电机库实战:从状态机到稳健启动流程的深度解析

本文深度解析了ST电机库中状态机的核心作用与稳健启动流程。状态机作为电机控制的“交通指挥官”,通过IDLE、CHARGE_BOOT_CAP、ALIGNMENT等一系列状态的严格跳转,确保了永磁同步电机(PMSM)和直流无刷电机(BLDC)的安全启动与可靠运行,是避免硬件损坏和实现平稳控制的关键。

weixin_29161785的博客 228

Find All Anagrams in a String

判断字符串是否相同字符、ASCII的hash表、滑动窗口已经边界值的判断和还原。

不积跬步无以至千里 254

论文阅读——Bayesian Knowledge Fusion(贝叶斯知识融合)

该篇论文解决了不确定环境中的信息融合问题。想象一下,有多位专家针对同一情况构建概率模型,我们希望汇总他们提供的信息。直接合并每个信息可能会遇到几个问题。例如,专家们可能不同意某个事件发生的概率,或者他们可能不同意两个事件之间因果关系的方向(例如,一个人认为 A 导致 B,而另一个人认为 B 导致 A)。他们甚至可能不同意概率网络中一组变量之间的整个依赖结构。

qq_64164071的博客 1499

Verilog-asm小练习—PLL建模

Verilog- ams是Verilog标准的模拟混合信号语言,以下是对PLL建模的一种思路参考

weixin_44700185的博客 1019

438. Find All Anagrams in a String 寻找字符串种所有的同位串

438. Find All Anagrams in a String 寻找字符串种所有的同位串 先说一下题意,给一个字符串S,和一个模式串P,要求找到所有的P的同位串(Anagram)在S种的起始Index,Anagram的意思就是组成字符相同的字符串。这一道题有很多的解法,有一些时间复杂度比较高,这里我们就不多赘述了,就讲一种比较简单,代码量也不大而且时间复杂度为O(N)的解法。 这一种方法主要是使用了滑动窗口和一个字符哈希数组来解决。 主要思维为: 1.新建一个256的数组用来存放当前窗口内所有的字符的

Dh0le的博客 401

别再只用线性回归了!用sklearn的核岭回归搞定非线性数据(附RBF核调参实战)

本文详细介绍了如何使用sklearn的核岭回归处理非线性数据,特别是RBF核的应用与调参实战。通过网格搜索找到最佳参数组合,核岭回归能有效捕捉数据中的复杂波动,显著提升模型性能。适合需要解决非线性问题的数据科学家和机器学习工程师。

weixin_34246529的博客 339

LeetCode //C - 438. Find All Anagrams in a String

【代码】LeetCode //C - 438. Find All Anagrams in a String。

Made in Code 1155

8-阿里云服务器 ECS配置R及Studio Server

步骤概览:(首先要明确自己的系统然后进行安装)

hx2024的博客 1572

YOLOv11改进 | 检测头篇 | 利用动态蛇形卷积DySnakeConv改进yolov11分割检测头(全网独家首发,Segment)

本文给大家带来的最新改进机制是一种我进行优化的专用于分割的检测头,在分割的过程中,最困难的无非就是边缘的检测,动态蛇形卷积(Dynamic Snake Convolution)通过自适应地聚焦于细长和迂回的局部结构,准确地捕捉管状结构的特征。这种卷积方法的核心思想是,通过动态形状的卷积核来增强感知能力,针对管状结构的特征提取进行优化,所以将这个卷积针对于YOLOv11的分割头进行融合是非常合适的,当然本文的检测头也支持用于目标检测,但是我将其设计出来是主要为了分割的读者使用的。

Snu77的博客 3433

【Lintcode】647. Find All Anagrams in a String

题目地址: https://www.lintcode.com/problem/find-all-anagrams-in-a-string/description 给定两个字符串sss和ppp,如果ppp的某个排列能成为sss的子串,则记录下这个子串的第一个字母的下标。返回所有这样的下标。题目保证字符串只含小写英文字母。 思路和https://blog.youkuaiyun.com/qq_46105170/article/details/106140361类似,不同的地方只在于本题需要返回所有位置,所以每个位置都要判断一

数学、算法爱好者的博客 158

AMBA-CHI协议详解(七)

本节描述该协议包含的支持系统保序需求的机制。

Crazzy_M的博客 1161

Lintcode 647. Find All Anagrams in a String (Easy) (Python)

647. Find All Anagrams in a String Description: Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowercase English letters only and ...

DY的博客 291

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

数据结构与算法:Find All Anagrams in a String

给出两个字符串s和p,当s中存在p的异构串时,返回所有s中对应的所有起点位置。

m0_37564426的博客 55

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode算法题-Find All Anagrams in a String(Java实现)

这是悦乐书的第228次更新,第240篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第95题(顺位题号是438)。给定一个字符串s和一个非空字符串p,找到s中p的字谜的所有起始索引。字符串仅由小写英文字母组成,字符串s和p的长度不会大于20,100。输出顺序无关紧要。例如: 输入:s:“cbaebabacd” p:“abc” 输出:[0,6] 说明: 起始索引等于0的...

weixin_34249678的博客 161

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

438. Find All Anagrams in a String

题目: Given two stringssandp, returnan array of all the start indices ofp's anagrams ins. You may return the answer inany order. Example 1: Input: s = "cbaebabacd", p = "abc" Output: [0,6] Explanation: The substring with start index = 0 is "cba...

weixin_49991368的博客 162

【滑动窗口精选题目】详解8道题

滑动窗口经典8道题:1、长度最小的子数组 2、无重复字符的最长子串 3、最大连续1的个数 III 4、将x减小到0的最小操作数 5、水果成篮 6、找到字符串中所有字母异位词 7、串联所有单词的子串 8、最小覆盖子串

m0_74044018的博客 1967

滑动窗口经典题目

滑动窗口经典题目

脑子不好的小菜的博客 1341

算法: 滑动窗口题目练习

做这道题时,脑子里大概有个印象,知道要用滑动窗口,但是对于滑动窗口为什么就对,不会落情况吗?对于这一点不是很清楚.看完题解,发现我的代码只需要把一个if改成while就能过了,而且速度很快…刚开始写时没写对,后来画了两次图,根据图,然后就写出来了~看了题解,跟我写的差不多,但是我写的还有可以优化的地方.

月临水的博客 2828

leetcode:Find All Anagrams in a String 滑动窗口方法总结

今天做了几道滑动窗口的题,稍微总结一下。 起因源于早上在leetcode上pick one,随机到了一个easy的题目,想着随便做了,结果半天也找不到最优解,耗时300多ms,A是A了,不过就是暴力罢了。 题目是:Find All Anagrams in a String,链接在https://leetcode.com/problems/find-all-anagrams-in-a-string

yy254117440的博客 9340

leetcode438. Find All Anagrams in a String

@小花 题目描述: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p

无忌的博客 3505

LeetCode:438. Find All Anagrams in a String

最近又开始刷LeetCode了,很多之前做过的题目这次很快地做出来了。但是,今天遇到了Find All Anagrams in a String这题,又琢磨了好久,可能是没理解题目的精髓所在。my bad solution我的解特别low,使用HashMap,如下: public List<Integer> findAnagrams(String s, String p) {

ting说你跳? 514

[LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

  Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be la...

weixin_33779515的博客 181

Leetcode 438. Find All Anagrams in a String 找变位子串 解题报告

1 解题报告这道题,其实也就是要求从S中找到一个子串,这个子串变化后可以得到P(其实就是这个子串的26字母的分布和P一样)所以自然而然的,我使用了最naïve的方式,就是直接滑动窗口(长度为P的大小),检查S的每一个子串的分布情况和P是否一样就可以了。当然还可以优化检查的流程,但是既然AC了,我就不优化了~~~2 原题Given a string s and a non-empty string p

MebiuW的专栏 4068

leetcode (Find All Anagrams in a String)

Title:Find All Anagrams in a String   438 Difficulty:Easy 原题leetcode地址:  https://leetcode.com/problems/find-all-anagrams-in-a-string/   1.  见代码注释 时间复杂度:O(n),嵌套循环。 空间复杂度:O(n),申请Map和List。 /*...

hsx1612727380的专栏 171
上一篇: LintCode 646:First Position Unique Character
下一篇: LintCode 661: Convert BST to Greater Tree (BST好题)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值