30. Substring with Concatenation of All Words
This commit is contained in:
36
30. Substring with Concatenation of All Words/Solution.cs
Normal file
36
30. Substring with Concatenation of All Words/Solution.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
public class Solution {
|
||||
public IList<int> FindSubstring(string s, string[] words) {
|
||||
var len = words[0].Length;
|
||||
var allLen = len*words.Length;
|
||||
var dict = new Dictionary<string, int>();
|
||||
foreach(var word in words)
|
||||
{
|
||||
dict.TryAdd(word, 0);
|
||||
dict[word]++;
|
||||
}
|
||||
var answer = new List<int>();
|
||||
var dictClone = new Dictionary<string, int>(dict);
|
||||
for(var i = 0; i < s.Length-allLen+1; i++)
|
||||
{
|
||||
var valid = 0;
|
||||
foreach (var kvp in dictClone)
|
||||
dictClone[kvp.Key] = dict[kvp.Key];
|
||||
for(var j=i; j < i+allLen; j+=len)
|
||||
{
|
||||
var word = s[j..(j+len)];
|
||||
if(dictClone.TryGetValue(word, out var count) && count > 0)
|
||||
{
|
||||
dictClone[word]--;
|
||||
valid++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(valid == words.Length)
|
||||
answer.Add(i);
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user