【leetcode刷题笔记】Substring with Concatenation of All Words

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

For example, given:
S"barfoothefoobarman"
L["foo", "bar"]

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



题解:题目的意思是在S中找到一个子串,恰好包含了L中所有的串,L中的串在S的字串中的顺序不重要。

思路很简单,假设L中共有m个串,每个串长度为n,那么L中子串合并起来总长度是m*n,那么只要在S中依次搜索长度为m*n的串就可以了。在搜索的过程中,设置两个hashmap,一个存放L中的串和它们在L中出现的次数,一个存放在S中m*n的子串中找到的长度为n的串和它们在S的子串中出现的次数,因为查看的是S长度为m*n的子串,并且是n个字符为一组查看的,所以要么在S中看到某个长度为n的子串不出现在L中,要么在S中出现的次数比L中多,否则这个长度为m*n的串就是L的所有串的合并。

例如题目中的例子

  • 我们首先查看S的子串barfoo,查看这个子串的时候,按照bar,foo的顺序查看,得知子串foobar是符合要求的
  • 再查看子串arfoot,查看顺序是arf,oot,发现arf不在L中,所以arfoot不符合要求;
  • 再查看子串rfooth,......
 1         if(L == null || L.length == 0)
 2             return null;
 3         int m = L.length;
 4         int n = L[0].length();
 5         //store n-length strings in L
 6         HashMap<String, Integer> map = new HashMap<String, Integer>();
 7         //store n-length strings inS
 8         HashMap<String, Integer> InS = new HashMap<String, Integer>();
 9         List<Integer> answer = new ArrayList<Integer>();
10         for(String s:L){
11             if(!map.containsKey(s))
12                 map.put(s, 1);
13             else {
14                 map.put(s, map.get(s)+1);
15             }
16         }
17
18
19         for(int i = 0;i <= S.length() - m*n;i++){
20             InS.clear();
21             boolean find = true;
22             for(int j = 0;j < m;j++){
23                 String sub = S.substring(i+j*n,i+(j+1)*n);
24                 //if a n-length string in S‘s substring doesn‘t in L, skip to search a new substring in S
25                 if(!map.containsKey(sub)){
26                     find = false;
27                     break;
28                 }
29                 if(!InS.containsKey(sub))
30                     InS.put(sub, 1);
31                 else {
32                         InS.put(sub, InS.get(sub)+1);
33                 }
34                 //if a n-length string in S‘substring appears more time than in L, stop checking this substring
35                 if(InS.get(sub) > map.get(sub)){
36                     find = false;
37                     break;
38                 }
39             }
40             if(find)
41                 answer.add(i);
42         }
43         return answer;

【leetcode刷题笔记】Substring with Concatenation of All Words,布布扣,bubuko.com

时间: 2024-10-12 03:37:37

【leetcode刷题笔记】Substring with Concatenation of All Words的相关文章

【leetcode刷题笔记】Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【leetcode刷题笔记】Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a","a","

【leetcode刷题笔记】Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

【leetcode刷题笔记】Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

【leetcode刷题笔记】Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T