Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8fd268f

Browse files
author
applewjg
committed
SubstringwithConcatenationofAllWords
Change-Id: Icd46a57e44dd56f293c15c2961cc3b5465cf27d3
1 parent9392afa commit8fd268f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Substring with Concatenation of All Words
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
7+
Notes:
8+
You are given a string, S, and a list of words, L, that are all of the same length. Find all
9+
starting indices of substring(s) in S that is a concatenation of each word in L exactly once
10+
and without any intervening characters.
11+
For example, given:
12+
S: "barfoothefoobarman"
13+
L: ["foo", "bar"]
14+
You should return the indices: [0,9].
15+
(order does not matter).
16+
17+
Solution: ...
18+
*/
19+
publicclassSolution {
20+
publicList<Integer>findSubstring(StringS,String[]L) {
21+
List<Integer>res =newArrayList<Integer>();
22+
if(L.length==0 ||S==null ||S.length()==0)returnres;
23+
intM =S.length(),N =L.length;
24+
intK =L[0].length();
25+
HashMap<String,Integer>need =newHashMap<String,Integer>();
26+
for(Stringstr:L) {
27+
if(need.containsKey(str)) {
28+
need.put(str,need.get(str)+1);
29+
}else {
30+
need.put(str,1);
31+
}
32+
}
33+
for (inti =0;i <=M -N*K; ++i) {
34+
HashMap<String,Integer>found =newHashMap<String,Integer>();
35+
intj =0;
36+
for (j =0;j <N; ++j) {
37+
Strings =S.substring(i +j *K,i + (j+1) *K);
38+
if (need.containsKey(s) ==false)break;
39+
if (found.containsKey(s) ==true) {
40+
if (need.get(s) <=found.get(s))break;
41+
found.put(s,found.get(s)+1);
42+
}elsefound.put(s,1);
43+
}
44+
if (j ==N)res.add(i);
45+
}
46+
returnres;
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp