LeetCode 438. 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 20,100.
The order of output does not matter.
Example 1:
Input:
s: "cbaebabacd" p: "abc" Output:
[0, 6] Explanation:
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".
Example 2:
Input:
s: "abab" p: "ab" Output:
[0, 1, 2] Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
题目标签:Hash Table
题目给了我们两个string s 和 p, 让我们在 s 中 找到所有 p 的变位词。
利用两个HashMap 和 Sliding window:
先把 p 的char 和 出现次数 存入 mapP;
然后遍历string s,利用 sliding window 把 和 p 一样长度的 string 的 char 保存在 tempMap 里,比较 tempMap 和 mapP。
Java Solution:
Runtime beats 20.00%
完成日期:11/07/2017
关键词:HashMap
关键点:利用sliding window 把 tempMap 和 mapP 比较
class Solution
{
public List<Integer> findAnagrams(String s, String p)
{
List<Integer> list = new ArrayList<>();
HashMap<Character, Integer> mapP = new HashMap<>();
HashMap<Character, Integer> tempMap = new HashMap<>(); for(char c: p.toCharArray()) // store p char and occurrence into mapP
mapP.put(c, mapP.getOrDefault(c, 0) + 1); for(int i=0; i<s.length(); i++) // iterate s and update a tempMap with p len
{
char c = s.charAt(i);
char leftC; tempMap.put(c, tempMap.getOrDefault(c, 0) + 1); if(i >= p.length()) // once reach to p's length, remove most left char
{
leftC = s.charAt(i - p.length());
// remove left char
if(tempMap.get(leftC) == 1)
tempMap.remove(leftC);
else
tempMap.put(leftC, tempMap.get(leftC) - 1);
} if(tempMap.equals(mapP))
list.add(i + 1 - p.length()); } return list;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)的更多相关文章
- [LeetCode] 438. 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 ...
- [leetcode]438. 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 ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- [LeetCode] 438. Find All Anagrams in a String_Easy
438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution Pick One Given a str ...
- hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)
string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
随机推荐
- ionic3带参数返回原来页面
最近用ionic3+angular4做项目.我遇到了个问题,我返回原来页面时一般都会调用this.navCtrl.pop()方法,但这个方法不能携带参数.怎么办? 可以写个回调方法. 我在a页面定义个 ...
- python自动化--语言基础五面向对象、迭代器、range和切片的区分
面向对象 一.面向对象简单介绍: class Test(): #类的定义 car = "buick" #类变量,定义在类里方法外,可被对象直接调用,具有全局效果 def __ini ...
- 用PHP开发自己的独立博客(一)——概述
开篇废话:因为重新回归朝九晚五的生活,于是就想开始写技术博客,当是做技术文档了.于是试用了各类博客,CSDN.cnblogs都还不错.简单试用了一下,说说各自的特点. CSDN的界面不能定制,使用默认 ...
- 【sqli-labs】 less61 GET -Challenge -Double Query -5 queries allowed -Variation4 (GET型 挑战 双查询 只允许5次查询 变化4)
http://192.168.136.128/sqli-labs-master/Less-61/?id=1' 单引号双括号闭合 192.168.136.128/sqli-labs-master/Les ...
- pringboot开启找回Run Dashboard
代码中加入 <option name="configurationTypes"> <set> <option value="SpringBo ...
- nginx配置X-Forwarded-For 防止伪造ip
网上常见nginx配置ip请求头 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 风险: 用于可以通过自己设置请求头来伪造ip ...
- spark学习(1)---dataframe操作大全
一.dataframe操作大全 https://blog.csdn.net/dabokele/article/details/52802150 https://www.jianshu.com/p/00 ...
- 【webpack插件使用】在开发中快速掌握并使用Webpack构建web应用程序
1.webpack-dev-server插件的基本使用 入门程序 const path = require('path'); // 导出一个Webpack的配置对象(通过node中的模块操作,向外暴露 ...
- LINUX-磁盘空间
df -h 显示已经挂载的分区列表 ls -lSr |more 以尺寸大小排列文件和目录 du -sh dir1 估算目录 'dir1' 已经使用的磁盘空间' du -sk * | sort -rn ...
- stm32实现iap远程固件更新
前提 想来做iap升级了,应该不是什么新手. 下面的程序需要用到一些简单的功能 串口收发数据开关总中断虽然本文标题是实现远程固件更新,但是具体远程方案本文不做详细说明,重点在于介绍mcu接收到新的固件 ...