From 80c10a32664975adc99096d8be799ac439fd579a Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 13 Aug 2021 16:10:50 +0800 Subject: [PATCH] =?UTF-8?q?438:=E6=89=BE=E5=88=B0=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E6=89=80=E6=9C=89=E5=AD=97=E6=AF=8D=E5=BC=82?= =?UTF-8?q?=E4=BD=8D=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FindAllAnagramsInAString.java | 84 +++++++++++++++++++ .../editor/cn/FindAllAnagramsInAString.md | 36 ++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.java create mode 100644 src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.md diff --git a/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.java b/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.java new file mode 100644 index 0000000..ed73dfc --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.java @@ -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 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 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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.md b/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.md new file mode 100644 index 0000000..e0cbd45 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FindAllAnagramsInAString.md @@ -0,0 +1,36 @@ +

给定两个字符串 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
  • 哈希表
  • 字符串
  • 滑动窗口
  • \n
  • 👍 586
  • 👎 0
  • \ No newline at end of file