find-all-anagrams-in-a-string
https://leetcode.com/problems/find-all-anagrams-in-a-string/
package com.company; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; class Solution {
public List<Integer> findAnagrams(String s, String p) {
int[] smap = new int[26];
int[] pmap = new int[26];
int plen = p.length();
int slen = s.length(); for (int i=0; i<plen; i++) {
pmap[p.charAt(i)-'a']++;
} List<Integer> lst = new ArrayList<>();
int count = 0;
for (int i=0; i<plen&&i<slen; i++) {
int xx = s.charAt(i) - 'a';
if (pmap[xx] > 0) {
smap[xx]++;
if (smap[xx] <= pmap[xx]) {
count++;
}
}
}
if (count == plen) {
lst.add(0);
}
for (int i=plen; i<slen; i++) {
int yy = s.charAt(i-plen) - 'a';
if (smap[yy] > 0) {
smap[yy]--;
if (smap[yy] < pmap[yy]) {
count--;
}
}
int xx = s.charAt(i) - 'a';
if (pmap[xx] > 0) {
smap[xx]++;
if (smap[xx] <= pmap[xx]) {
count++;
}
}
if (count == plen) {
lst.add(i-plen+1);
}
}
return lst;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello");
Solution solution = new Solution(); List<Integer> ret = solution.findAnagrams("abab", "ab");
System.out.printf("Result is len %d\n", ret.size());
Iterator<Integer> iter = ret.iterator();
while (iter.hasNext()) {
System.out.printf("%d;", iter.next());
}
System.out.println(); }
}
find-all-anagrams-in-a-string的更多相关文章
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- 【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 - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- [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 ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- [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 c ...
- [Swift]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 ...
- [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 ...
- 10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
Level: Easy 题目描述: Given a string s and a non-empty string p, find all the start indices of p's ana ...
随机推荐
- map线程
来看看map线程到底是如何运行的 很早就知道一个map是一个线程,以后有可能改成一个map一个进程,那就先来看看一个map一个线程是如何运作的 其实刚开始整个服务器就是两个线程,但发现这样服务器支持的 ...
- 使用WCF服务的客户端出现maxReceivedMessageSize异常
使用WCF服务的客户端出现maxReceivedMessageSize异常解决方案 当使用WCF的客户端调取的数据过多时,会出现这个异常.一般情况下,系统默认值是65536,大约容纳100-200条左 ...
- poj 2187
求凸包后枚举凸包上的点 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- mysql同时修改2个表思路
1.需求:修改评论表中的昵称为手机号码最后4位. ,) AND issuer_name LIKE '1%'; 2.由于误操作(MID(issuer_name,4,6)是中间的6位),需要数据回滚. 3 ...
- HDU2295 Radar (DLX)
下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...
- 最长连续回文串(最优线性时间O(n))
转自:http://blog.csdn.net/hopeztm/article/details/7932245 Given a string S, find the longest palindrom ...
- 替代jquery
如果不需要过多操作,不引用jquery 1.document.ready :$(function(){}) http://www.cnblogs.com/a546558309/p/3478344.ht ...
- BroadcastReceiver应用1
有两种注册方式:1. 在AndroidManifest中注册.2. 在代码中直接注册,这种注册需要注意的一点是:当注册此Receiver的Activity退出的时候,一定要调用unregisterRe ...
- Good Bye 2015 A. New Year and Days 签到
A. New Year and Days Today is Wednesday, the third day of the week. What's more interesting is tha ...
- 【poj3358】消因子+BSGS 或 消因子+欧拉定理 两种方法
题意:给你一个分数,求它在二进制下的循环节的长度,还有第一个循环节从哪一位开始. For example, x = 1/10 = 0.0001100110011(00110011)w and 0001 ...