摘要:解題思路,就是只順序不同但個(gè)數(shù)相同的字符串,那我們就可以利用的思想來比較每個(gè)字符串中字符出現(xiàn)的個(gè)數(shù)是否相等。
Find All Anagrams in a String
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".
1.解題思路
anagrams,就是只順序不同但個(gè)數(shù)相同的字符串,那我們就可以利用hashtable的思想來比較每個(gè)字符串中字符出現(xiàn)的個(gè)數(shù)是否相等。
對于兩個(gè)字符串我們分別準(zhǔn)備數(shù)組(大小為256)來存儲(chǔ)每個(gè)字符出現(xiàn)的次數(shù):
1) 對于p,我們遍歷,并在hp中記錄字符出現(xiàn)的次數(shù);
2) 之后遍歷s,先把當(dāng)前字符的個(gè)數(shù)+1,但是需要考慮當(dāng)前index是否已經(jīng)超過了p的長度,如果超過,則表示前面的字符已經(jīng)不予考慮,所以要將index-plen的字符的個(gè)數(shù)-1;最后判斷兩個(gè)數(shù)組是否相等,如果相等,返回index-plen+1,即為開始的下標(biāo)。
2.代碼
public class Solution { public ListfindAnagrams(String s, String p) { List res=new ArrayList (); if(s.length()==0||s==null||p.length()==0||p==null) return res; int[] hs=new int[256]; int[] hp=new int[256]; int plen=p.length(); for(int i=0;i =plen){ hs[s.charAt(j-plen)]--; } if(Arrays.equals(hs,hp)) res.add(j-plen+1); } return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/69799.html
摘要:題目要求思路和代碼這是一個(gè)簡單的雙指針問題,即左指針指向可以作為起點(diǎn)的子數(shù)組下標(biāo),右指針則不停向右移動(dòng),將更多的元素囊括進(jìn)來,從而確保該左右指針內(nèi)的元素是目標(biāo)數(shù)組的一個(gè)兄弟子數(shù)組即每個(gè)字母的個(gè)數(shù)均相等左指針記錄每個(gè)字母出現(xiàn)的次數(shù)拷貝一個(gè) 題目要求 Given a string s and a non-empty string p, find all the start indices ...
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...
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 ...
摘要:同時(shí)使用方法將數(shù)組轉(zhuǎn)化為并利用的直接比較兩個(gè)字符串是否相等。通過這種方法效率值提高了不少。 題目要求 Given an array of strings, group anagrams together. For example, given: [eat, tea, tan, ate, nat, bat], Return: [ [ate, eat,tea], [nat,t...
摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...
閱讀 3730·2021-11-24 09:39
閱讀 1891·2021-11-16 11:45
閱讀 620·2021-11-16 11:45
閱讀 1040·2021-10-11 10:58
閱讀 2482·2021-09-09 11:51
閱讀 1946·2019-08-30 15:54
閱讀 695·2019-08-29 13:13
閱讀 3472·2019-08-26 12:18