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的更多相关文章

  1. 【leetcode】438. Find All Anagrams in a String

    problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...

  2. 438. Find All Anagrams in a String

    原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...

  3. 【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 ...

  4. 438. Find All Anagrams in a String - LeetCode

    Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...

  5. [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 ...

  6. LeetCode Find All Anagrams in a String

    原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...

  7. [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 ...

  8. [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 ...

  9. [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. 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 ...

随机推荐

  1. javascript 字符串和json的互转

    FROM:http://www.cnblogs.com/mizzle/archive/2012/02/10/2345891.html 在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操 ...

  2. [HTML/CSS]display:none和visibility:hidden的区别

    写在前面 在群里有朋友问这样一个问题,display:none的标签,影响了布局.这就引出了本篇这样的问题,印象中display:none的块元素是不占位置的. 一个例子 <!DOCTYPE h ...

  3. URAL题解—不断跟新中

    1014:简单题,忘了0的情况可以是10,== 1219:找呀找规律,满足N*(N-1)/2+1=X;就是1 的情况了

  4. asp.net的sessionState节点详解

    web.config关于sessionState节点的配置方案,sessionState有四种模式:off,inProc,StateServer,SqlServer. 1.off模式 从字面上就可以看 ...

  5. 停止使用循环 教你用underscore优雅的写代码

    你一天(一周)内写了多少个循环了? var i; for(i = 0; i < someArray.length; i++) {   var someThing = someArray[i]; ...

  6. js函数延迟执行

    function delay(value){ //全局变量保存当前值 window._myTempDalayValue = value; setTimeout(function(){ //延时之后与全 ...

  7. xxx.properties获取方法

    1.手动获取加载 Locale locale = Locale.getDefault(); ResourceBundle localResource = ResourceBundle.getBundl ...

  8. jmeter学习预热

    对jmeter有些兴趣,最近想抽点时间熟悉下,个人熟悉某个工具的方法: 1.官网http://jmeter.apache.org/,一般官网会有Tutorials或者Getting Started之类 ...

  9. 2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

    题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中 ...

  10. tomcat简介及原理解说

    Tomcat简介 作者:杨晓(http://blog.sina.com.cn/u/1237288325) 目录: ----Tomcat背景 ----Tomcat目录 ----Tomcat类加载 --- ...