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. [工作积累] android 中添加libssl和libcurl

    1. libssl https://github.com/guardianproject/openssl-android 然后执行ndk-build 2.libcurl 源代码组织结构, 下面的mak ...

  2. 如何使用CSL(翻译总结自TI官方文档)

    为了使用CSL来进行编译和连接,必须先配置CCS开发环境. 1.指定目标设备 Project/options/complier/preprocessor,在define symbols中输入设备支持符 ...

  3. linux 访问ntfs分区

    打开ntfs-3g的下载点http://www.tuxera.com/community/ntfs-3g-download/ ,将最新稳定(当前最新版本为ntfs-3g-2011.1.15)下载到Ce ...

  4. ASP.NET MVC中几个运用技巧

    1. Razor Helpers 的运用:例如,定义好 ViewBag.Message = "Welcome to ASP.NET MVC!";我要在界面上显示"Welc ...

  5. java多线程基础知识

    1.ThrTest.java 继承Thread类方式 public class ThrTest extends Thread { private String name; public ThrTest ...

  6. POJ 1658

    #include<iostream>//cheng da cai zi using namespace std; int main() { int i; int time; ]; cin& ...

  7. VS输入法问题

    问题描述:启动VS,打开Winform等的界面设计,无法为控件输入中文,另外,运行程序,无法在TextBox等控件中输入中文: 本人的系统环境:Win7旗舰版,VS2008.VS2010和VS2012 ...

  8. Yarn中如何生成状态机图

    原文 http://xiguada.org/yarn_state_picture/ 在Hadoop2.0系列的版本里,采用了状态机的方式处理ResourceManager,NodeManager,Ma ...

  9. ffmpeg转码MPEG2-TS的音视频同步机制分析

    http://blog.chinaunix.net/uid-26000296-id-3483782.html 一.FFmpeg忽略了adaptation_field()数据 FFmpeg忽略了包含PC ...

  10. 谈谈三层架构中Model的作用

    Model又叫实体类,这个东西,大家可能觉得不好分层.包括我以前在内,是这样理解的:UI<-->Model<-->BLL<-->Model<-->DAL ...