力扣题目之串联所有单词的子串

本文介绍了LeetCode中一题关于字符串的困难问题——找到串联所有给定单词的子串起始位置。作者分享了首次尝试使用全排列算法导致超时的经历,最终采用双Map解决方案,通过记录单词中字符出现次数和子字符串出现情况,成功解决该问题。
摘要由CSDN通过智能技术生成

力扣题目之串联所有单词的子串

哈哈我胡汉三又回来了,时隔不知多少天我又回来写博客了,自我感动ing。废话不多说,这次的题目是LeetCode上的关于字符串的困难题目,说实话每次看到困难两个字我心里就打上退堂鼓了,不过不试试怎么能知道呢,咸鱼还有梦想呢。然后我A了上去并且勇敢的打出了GG。

串联所有单词的子串

给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

输入:
s = “barfoothefoobarman”,
words = [“foo”,“bar”]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 “barfoo” 和 “foobar” 。
输出的顺序不重要, [9,0] 也是有效答案。

这个题目我第一次看毫无头绪几乎要崩溃,但是我还是想了很久,没想到最后还真的想出一个好像能解决的算法—全排列,不知道各位有没有了解过这个算法,简单来说就是把所有排列的可能性列出来的一个算法,我当时就像查找字符串是否为给定字符串的子串的话那我就把全部组合的字符串列出来再一个个查找不就好了吗,然后我就开心的写了,最后LeetCode给了我一个超出时间!果然O(mn)的算法就不应该存在吗。最后还是通过了LeetCode的题解解决了问题,现在把他记录下来,用来提醒我下次再写O(mn)就把这个桌子吃下去。
新算法的特点就是用两个map,一个记录words里面单个字符出现的次数,另一个记录s里面每个同words里字符长度相同子字符串是否跟第一个map里面出现过,出现就在第二个里面保存并加1。如果超出第一个次数或者不存在则跳出循环跳过这里,如果循环正常结束那么正确答案就是for循环里的下标。

	public List<int> findSubstring(string s, string[] words) 
	{
		List<int> res = new List<int>();
        int wordNum = words.Length;
        if (wordNum == 0)
            return res;
        Dictionary<string, int> allWords = new Dictionary<string, int>();
        foreach (var item in words)
        {
            int value;
            allWords.TryGetValue(item, out value);
            allWords[item] = ++value;
        }
        int wordLen = words[0].Length;

        for (int i = 0; i < wordLen; i++)
        {
            Dictionary<string, int> hasWords = new Dictionary<string, int>();
            int num = 0;
            for (int j = i; j < s.Length - wordNum * wordLen + 1; j += wordLen)
            {
                bool isRemoved = false;
                while (num< wordNum)
                {
                    string word = s.Substring(j+num*wordLen, wordLen);
                    if (allWords.ContainsKey(word))
                    {
                        int value;
                        hasWords.TryGetValue(word, out value);
                        hasWords[word] = ++value;
                        if (hasWords[word] > allWords[word])
                        {
                            isRemoved = true;
                            int removeNum = 0;
                            while (hasWords[word] > allWords[word])
                            {
                                string firstWord = s.Substring(j + removeNum * wordLen, wordLen);
                                int v = hasWords[firstWord];
                                hasWords[firstWord] = v - 1;
                                removeNum++;
                            }
                            num = num - removeNum + 1;
                            j = j + (removeNum - 1) * wordLen;
                            break;
                        }
                    }
                    else
                    {
                        hasWords.Clear();
                        j = j + num * wordLen;
                        num = 0;
                        break;
                    }
                    num++;
                }
                if (num == wordNum)
                {
                    res.Add(j);
                }
                if (num > 0 && !isRemoved)
                {
                    string firstWord = s.Substring(j, wordLen);
                    int v;
                    hasWords.TryGetValue(firstWord, out v);
                    hasWords[firstWord] = v - 1;
                    num--;
                }
            }
        }
        return res;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值