forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
57 lines (52 loc) · 1.59 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Collections.Generic;
public class Solution {
public IList<int> FindSubstring(string s, string[] words) {
if (words.Length == 0) return new List<int>();
var wordsDict = new Dictionary<string, int>();
foreach (var word in words)
{
if (!wordsDict.ContainsKey(word))
{
wordsDict.Add(word, 1);
}
else
{
++wordsDict[word];
}
}
var wordOfS = new string[s.Length];
var wordLength = words[0].Length;
var wordCount = words.Length;
for (var i = 0; i <= s.Length - wordLength; ++i)
{
var substring = s.Substring(i, wordLength);
if (wordsDict.ContainsKey(substring))
{
wordOfS[i] = substring;
}
}
var result = new List<int>();
for (var i = 0; i <= s.Length - wordLength * wordCount; ++i)
{
var tempDict = new Dictionary<string, int>(wordsDict);
var tempCount = 0;
for (var j = i; j <= i + wordLength * (wordCount - 1); j += wordLength)
{
if (wordOfS[j] != null && tempDict[wordOfS[j]] > 0)
{
--tempDict[wordOfS[j]];
++tempCount;
}
else
{
break;
}
}
if (tempCount == wordCount)
{
result.Add(i);
}
}
return result;
}
}