摘要:題目要求思路和代碼這是一個簡單的雙指針問題,即左指針指向可以作為起點的子數(shù)組下標,右指針則不停向右移動,將更多的元素囊括進來,從而確保該左右指針內(nèi)的元素是目標數(shù)組的一個兄弟子數(shù)組即每個字母的個數(shù)均相等左指針記錄每個字母出現(xiàn)的次數(shù)拷貝一個
題目要求
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 20,100. The order of output does not matter. Example 1: Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: 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". Example 2: Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is an anagram of "ab".思路和代碼
這是一個簡單的雙指針問題,即左指針指向可以作為起點的子數(shù)組下標,右指針則不停向右移動,將更多的元素囊括進來,從而確保該左右指針內(nèi)的元素是目標數(shù)組的一個兄弟子數(shù)組(即每個字母的個數(shù)均相等)
public ListfindAnagrams(String s, String p) { List result = new ArrayList (); if(p==null || s==null || p.isEmpty() || s.isEmpty()) return result; //左指針 int startIndex = 0; //記錄每個字母出現(xiàn)的次數(shù) int[] map = new int[26]; for(char c : p.toCharArray()) { map[c-"a"]++; } char[] sArray = s.toCharArray(); //拷貝一個臨時map int[] tmpMap = Arrays.copyOf(map, map.length); for(int i = 0 ; i 0) { tmpMap[index]--; //如果左右指針中數(shù)組的長度等于目標數(shù)組長度,說明該子數(shù)組合法,將左指針加入結(jié)果集中 if(i - startIndex + 1 == p.length()) { result.add(startIndex); tmpMap[sArray[startIndex]-"a"]++; startIndex++;//左指針右移一位 } }else if(sArray[startIndex] == sArray[i]){//如果左右指針值相等,則左指針只向右移動一個 startIndex++; }else { //將左指針移動到當前右指針的位置上,因為中間部分的子數(shù)組一定無法構(gòu)成目標數(shù)組 startIndex = i--; tmpMap = Arrays.copyOf(map, map.length); } } return result; }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/74771.html
Problem Given a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be...
摘要:解題思路,就是只順序不同但個數(shù)相同的字符串,那我們就可以利用的思想來比較每個字符串中字符出現(xiàn)的個數(shù)是否相等。 Find All Anagrams in a StringGiven a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of...
摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學習。 這篇文章記錄我練習的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...
摘要:月下半旬攻略道題,目前已攻略題。目前簡單難度攻略已經(jīng)到題,所以后面會調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時,攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...
Problem Given an array of strings, group anagrams together. Example: Input: [eat, tea, tan, ate, nat, bat], Output: [ [ate,eat,tea], [nat,tan], [bat] ] Note: All inputs will be in lowercase.The ...
閱讀 3576·2021-11-16 11:45
閱讀 2149·2021-11-08 13:23
閱讀 2228·2021-10-11 10:59
閱讀 2906·2021-09-27 13:36
閱讀 2492·2019-08-30 15:54
閱讀 2684·2019-08-29 16:58
閱讀 2802·2019-08-29 16:56
閱讀 1351·2019-08-26 13:52