LeetCode_30---Substring with Concatenation of All Words

来源:互联网 发布:java 读取资源文件 编辑:程序博客网 时间:2024/05/23 12:34

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.

For example, given:
s"barfoothefoobarman"
words["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

Hide Tags
 Hash Table Two Pointers String
Have you met this question in a real interview? 
Yes
 
No

URL:地址

Code:

package From21;import java.util.ArrayList;import java.util.HashMap;import java.util.List;/** * @author MohnSnow * @time 2015年6月15日 下午1:29:12 * @from http://m.blog.csdn.net/blog/yapian8/28095025 * @translate 变相的字符串匹配给定一个字符串,然后再给定一组相同长度的单词列表, *            要求在字符串中查找满足以下条件的起始位置: *            1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次。 *            2)在出现的过程中不能出现其他的干扰单词。 *            3)出现的位置可能有多个。 *            4)单词的出现顺序不做要求。 */public class LeetCode30 {/** * @param argsmengdx *            -fnst */public static List<Integer> findSubstring(String S, String[] L) {// Note: The Solution object is instantiated only once and is reused by each test case.ArrayList<Integer> res = new ArrayList<Integer>();if (S == null || S.length() == 0 || L == null || L.length == 0)return res;HashMap<String, Integer> map = new HashMap<String, Integer>();for (int i = 0; i < L.length; i++)//统计字符串,以及其出现次数{if (map.containsKey(L[i])) {map.put(L[i], map.get(L[i]) + 1);} else {map.put(L[i], 1);}}System.out.println("map统计字符数组及其相应出现次数:" + map.toString());for (int i = 0; i < L[0].length(); i++){HashMap<String, Integer> curMap = new HashMap<String, Integer>();int count = 0;int left = i;for (int j = i; j <= S.length() - L[0].length(); j += L[0].length()){String str = S.substring(j, j + L[0].length());if (map.containsKey(str)){if (curMap.containsKey(str)) {curMap.put(str, curMap.get(str) + 1);} else {curMap.put(str, 1);}if (curMap.get(str) <= map.get(str)) {//出现重复字符串count++;} else {while (curMap.get(str) > map.get(str)) {String temp = S.substring(left, left + L[0].length());if (curMap.containsKey(temp)) {curMap.put(temp, curMap.get(temp) - 1);if (curMap.get(temp) < map.get(temp)) {count--;}}left += L[0].length();}}if (count == L.length) {res.add(left);String temp = S.substring(left, left + L[0].length());if (curMap.containsKey(temp))curMap.put(temp, curMap.get(temp) - 1);count--;left += L[0].length();}} else {curMap.clear();count = 0;left = j + L[0].length();}}}return res;}//brute force---708ms//可能的取值范围是 0~s.length()-words[0].length()*words.lengthpublic static List<Integer> findSubstring1(String s, String[] words) {int sLen = words[0].length();int wLen = words.length;ArrayList<Integer> res = new ArrayList<Integer>();if (s == null || s.length() == 0 || words == null || words.length == 0)return res;HashMap<String, Integer> map = new HashMap<String, Integer>();HashMap<String, Integer> curMap = new HashMap<String, Integer>();for (int i = 0; i < wLen; i++)//统计字符串数组中的字符以及其出现次数,避免重复。{if (map.containsKey(words[i])) {map.put(words[i], map.get(words[i]) + 1);} else {map.put(words[i], 1);}}for (int i = 0; i <= s.length() - sLen * wLen; i++) {curMap.clear();int j = i;while (j < i + sLen * wLen) {String str = s.substring(j, j + sLen);if (curMap.containsKey(str)) {curMap.put(str, curMap.get(str) + 1);} else {curMap.put(str, 1);}if (map.containsKey(str) && curMap.get(str) <= map.get(str)) {//出现重复字符串System.out.println("寻找j:" + j);j = j + sLen;} else {break;}}if (j == i + sLen * wLen) {res.add(i);}}return res;}public static void main(String[] args) {//String s = "asd";//String[] words = { "aaa", "asd", "fff", "sdf" };//s = "barfoothefoobarman";String s = "lingmindraboofooowingdingbarrwingmonkeypoundcake";String[] words = { "fooo", "barr", "wing", "ding", "wing" };//String s = "a";//String[] words = { "a" };System.out.println("算法一:" + findSubstring(s, words));System.out.println("算法二:" + findSubstring1(s, words));}}



0 0
原创粉丝点击