本文共 2114 字,大约阅读时间需要 7 分钟。
为了解决这个问题,我们需要找到字符串 s 中所有是字符串 p 的字母异位词的子串,并返回这些子串的起始索引。字母异位词是指字母相同但排列不同的字符串。
为了高效地解决这个问题,我们可以使用滑动窗口技术。滑动窗口技术允许我们在一个固定长度的窗口内高效地统计字符频率,并在每次滑动窗口时进行更新,从而在较短的时间内完成任务。
具体步骤如下:
p 中每个字符的频率,存储在 p_counts 数组中。current_counts,包含字符串 s 的前 m 个字符的频率,其中 m 是 p 的长度。s,每次滑动窗口时,移除左边界字符并加入右边界字符,更新 current_counts。p 的频率数组一致。如果一致,记录起始索引。这种方法的时间复杂度是 O(n),其中 n 是字符串 s 的长度,空间复杂度是 O(1)(固定 26 个字母)。
import java.util.ArrayList;import java.util.List;public class Solution_438 { public List findAnagrams(String s, String p) { int m = p.length(); int n = s.length(); List res = new ArrayList<>(); if (m > n) { return res; } int[] p_counts = new int[26]; for (int i = 0; i < m; i++) { p_counts[p.charAt(i) - 'a']++; } int[] current_counts = new int[26]; for (int i = 0; i < m; i++) { current_counts[s.charAt(i) - 'a']++; } for (int i = 0; i <= n - m; i++) { // 移除左边界字符 if (i > 0) { char outChar = s.charAt(i - 1); current_counts[outChar - 'a']--; if (current_counts[outChar - 'a'] < 0) { break; } } // 添加右边界字符 int inIndex = i + m; if (inIndex < n) { char inChar = s.charAt(inIndex); current_counts[inChar - 'a']++; } // 检查是否与p_counts匹配 boolean match = true; for (int j = 0; j < 26; j++) { if (current_counts[j] != p_counts[j]) { match = false; break; } } if (match) { res.add(i); } } return res; }} p_counts 数组记录 p 中每个字符的频率,current_counts 数组初始化为 p 的前 m 个字符频率。s,每次滑动窗口时,移除左边界字符并更新频率数组,然后加入右边界字符并更新频率数组。p 的频率数组一致。如果一致,记录起始索引。这种方法确保了在合理的时间复杂度内高效地找到所有符合条件的子串。
转载地址:http://vser.baihongyu.com/