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. 讨论下IDS的绕过

    自从知道dedecms自带了80sec的内置Mysqlids后,一直以来也没有想到绕过的办法.或者是自己mysql的根底太差了吧.于是分析dedecms源码时,只找模板执行,本地包含,上传等,完全没有 ...

  2. CSRF攻击之原理讲解

    |=——————————————————————=| |=————–=[ CSRF攻击原理解析 ]=——————=| |=——————————————————————=| |=——————-=[ By ...

  3. Python:Python 3.x 的革新

    Python 3.x 版本在设计时为了向最好的语言前进,没有考虑向下兼容,许多针对早期 Python 版本设计的程序都无法正常运行.本文简单介绍了 Python 3.x 版本较之 2.x 版本语法上的 ...

  4. hbase表结构设计

    非常好的一个ppt   HBase Schema design: http://www.slideshare.net/cloudera/5-h-base-schemahbasecon2012

  5. wifidog源码分析 - 初始化阶段

    Wifidog是一个linux下开源的认证网关软件,它主要用于配合认证服务器实现无线路由器的认证放行功能. wifidog是一个后台的服务程序,可以通过wdctrl命令对wifidog主程序进行控制. ...

  6. Unity3D的LightProbe动态光探头用法介绍

    原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201371721511106/ 之前曾经介绍过Unity3D的LightMappi ...

  7. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  8. POJ 1274

    #include<iostream> #include<stdio.h> #include <string.h> #include <vector> # ...

  9. OPEN资讯

    http://www.open-open.com/news/view/1f55540 随着 Android 平台市场份额的持续猛增 , 越来越多的开发者开始投入 Android 应用程序的开发大潮.如 ...

  10. TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE

    TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE 的区别 TASK_INTERRUPT ...