1 Star 0 Fork 0

agony/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
30.SubstringWithConcatenationOfAllWords.cs 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/agony2020/LeetCode.git
git@gitee.com:agony2020/LeetCode.git
agony2020
LeetCode
LeetCode
master

搜索帮助