438:找到字符串中所有字母异位词

This commit is contained in:
huangge1199 2021-08-13 16:10:50 +08:00
parent 72bd8f1c1d
commit 80c10a3266
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,84 @@
//给定两个字符串 s p找到 s 中所有 p 异位词 的子串返回这些子串的起始索引不考虑答案输出的顺序
//
// 异位词 指字母相同但排列不同的字符串
//
//
//
// 示例 1:
//
//
//输入: s = "cbaebabacd", p = "abc"
//输出: [0,6]
//解释:
//起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词
//起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词
//
//
// 示例 2:
//
//
//输入: s = "abab", p = "ab"
//输出: [0,1,2]
//解释:
//起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词
//起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词
//起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词
//
//
//
//
// 提示:
//
//
// 1 <= s.length, p.length <= 3 * 104
// s p 仅包含小写字母
//
// Related Topics 哈希表 字符串 滑动窗口
// 👍 586 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//438:找到字符串中所有字母异位词
class FindAllAnagramsInAString {
public static void main(String[] args) {
//测试代码
Solution solution = new FindAllAnagramsInAString().new Solution();
System.out.println(solution.findAnagrams("cbaebabacd", "abc"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<Integer> findAnagrams(String s, String p) {
if(p.length()>s.length()){
return new ArrayList<>();
}
int[] pchs = new int[26];
for (char ch : p.toCharArray()) {
pchs[ch - 'a']++;
}
char[] schs = s.toCharArray();
int start = 0;
int[] chs = new int[26];
List<Integer> list = new ArrayList<>();
for (int i = 0; i < p.length() - 1; i++) {
chs[schs[i] - 'a']++;
}
for (int i = p.length() - 1; i < schs.length; i++) {
chs[schs[i] - 'a']++;
if (Arrays.equals(pchs, chs)) {
list.add(start);
}
chs[schs[start] - 'a']--;
start++;
}
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,36 @@
<p>给定两个字符串 <code>s</code> 和 <code>p</code>,找到 <code>s</code><strong> </strong>中所有 <code>p</code><strong> </strong>的 <strong>异位词 </strong>的子串,返回这些子串的起始索引。不考虑答案输出的顺序。</p>
<p><strong>异位词 </strong>指字母相同,但排列不同的字符串。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入: </strong>s = "cbaebabacd", p = "abc"
<strong>输出: </strong>[0,6]
<strong>解释:</strong>
起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
</pre>
<p><strong> 示例 2:</strong></p>
<pre>
<strong>输入: </strong>s = "abab", p = "ab"
<strong>输出: </strong>[0,1,2]
<strong>解释:</strong>
起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code></li>
<li><code>s</code> 和 <code>p</code> 仅包含小写字母</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>滑动窗口</li></div></div>\n<div><li>👍 586</li><li>👎 0</li></div>