LintCode 647 Substring Anagrams
来源:互联网 发布:mac系统如何下载软件 编辑:程序博客网 时间:2024/05/17 22:12
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 40,000.
The order of output does not matter.
样例
Given
s = "cbaebabacd" p = "abc"return [0, 6]
The substring with start index = 0 is “cba”, which is an anagram of “abc”.
The substring with start index = 6 is “bac”, which is an anagram of “abc”.
题目的意思是给出字符串s和p,在s字符串中找出所有由p字符串颠倒字母顺序而构成的字符串,输出结果字符串的开始索引。
解一:
/** * @param s a string * @param p a non-empty string * @return a list of index */ public List<Integer> findAnagrams(String s, String p) { ArrayList<Integer> result = new ArrayList<>(); if (s == null || p == null) { return result; } int left = 0, right = 0, count = p.length(); int map[] = new int[256]; char[] sc = s.toCharArray(); for (char c : p.toCharArray()) { map[c]++; } while (right < s.length()) { if (map[sc[right++]]-- >= 1) { count--; } if (count == 0) { result.add(left); } if (right - left == p.length() && map[sc[left++]]++ >= 0) { count++; } } return result; }
阅读全文
0 0
- lintcode(647)Substring Anagrams
- LintCode 647 Substring Anagrams
- lintcode(round1)Substring Anagrams
- Substring Anagrams
- Substring Anagrams
- [LintCode]Anagrams(Python)
- Two Strings Are Anagrams-LintCode
- Lintcode - Longest common substring
- lintcode:Longest Common Substring
- [Lintcode]Minimum Window Substring
- [LintCode]Longest Common Substring(Python)
- LintCode:M-Longest Palindromic Substring
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- 「LintCode」java字符串練習四──anagrams
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- lintcode:Longest Substring Without Repeating Characters
- [LintCode 32] Minimum Window Substring(Python)
- Anagrams
- 《Java编程思想》混型
- C++基础之C与C++的联系和不同(三)
- servlet_1
- kotlin学习笔记3
- 顺时针打印矩阵
- LintCode 647 Substring Anagrams
- Linux常用命令(1)
- Python IO笔记
- ArrayList源码学习
- 230. Kth Smallest Element in a BST
- 计算机组成原理与系统结构 第一章计算机概述
- Qt5开发学习之事件(十三)
- 《C++ Concurrency in Action》笔记4 hardware_concurrency()
- Linux系统下FTP服务器的搭建