青岛城阳网站设计,免费网站成品,外贸网站建设费用,成都网站seo报价题目#xff1a;
给定两个字符串 s 和 p#xff0c;找到 s 中所有 p 的 异位词 的子串#xff0c;返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串#xff08;包括相同的字符串#xff09;。 代码#xff1a;
class Solution …题目
给定两个字符串 s 和 p找到 s 中所有 p 的 异位词 的子串返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串包括相同的字符串。 代码
class Solution {public ListInteger findAnagrams(String s, String p) {int n s.length(), m p.length();ListInteger res new ArrayList();if (n m)return res;int[] pCnt new int[26];int[] sCnt new int[26];for (int i 0; i m; i) {pCnt[p.charAt(i) - a];sCnt[s.charAt(i) - a];}if (Arrays.equals(sCnt, pCnt)) {res.add(0);}for (int i m; i n; i) {sCnt[s.charAt(i - m) - a]--;sCnt[s.charAt(i) - a];if(Arrays.equals(sCnt, pCnt)){res.add(i - m 1);}}return res;}
}