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. vector内存分配

    vector,map 这些容器还是在堆上分配的内存,在析构时是释放空间 vector在提高性能可以先reserve在push_back() reserve:决定capacity,但没有真正的分配内存, ...

  2. jquery中如何退出each循环

    在for循环中我们用continue退出当前循环,进入下一循环.用break跳出所有循环. 可是在jQuery中却并没有这两条命令,那么如何退出each循环呢? 经过查询得知: 在jQuery中用 r ...

  3. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  4. Request/Server的相关topic

    Request---------Server模式 HTTP 协议--------->这个可能返回json, 也可能是HTML HTML页面处理的流程以及资源文件的加载 浏览器最大连接数 js资源 ...

  5. 【转】CSS实现div的高度填满剩余空间

    转自:http://www.cnblogs.com/zhujl/archive/2012/03/20/2408976.html 高度自适应问题,我很抵触用js去解决,因为不好维护,也不够自然,但是纯用 ...

  6. MJRefresh插件引起的错误

    添加的头部或者尾部刷新,离开这个界面的时候需要移除 - (void)dealloc {    [_tableView removeHeader];} 不同版本的处理的方式不同 报的错误: 类的一个实例 ...

  7. group_concat

    分割字符串 group_concat(a.EvidencesID separator ',') as EvidencesID #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为 ...

  8. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  9. System.getProperty()参数

    java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装目 ...

  10. Linux之select系统调用_2

    在上一篇博文中,我们的程序中我们有3个客户端,因此也事先建立了3个管道,每个客户端分别使用一个管道向服务器发送消息.而在服务器端使用select系统调用,只要监测到某一管道有消息写入,服务器就将其re ...