30. 串联所有单词的子串

30. 串联所有单词的子串

原始题目链接:https://leetcode.cn/problems/substring-with-concatenation-of-all-words/

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

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

示例 1:

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

输入:s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
输出:[]
示例 3:

输入:s = “barfoofoobarthefoobarman”, words = [“bar”,“foo”,“the”]
输出:[6,9,12]

解题思路:

在s中以窗口大小开始查找words的单词是否都包含在内,是连续的并且都用完。

代码实现:

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        # 统计words里的单词及其个数
        words_dict = {}
        for word in words:
            words_dict[word] = words_dict.get(word, 0) + 1

        # words的单词长度,题目说明是每个单词长度是一样的
        word_len = len(words[0])
        # 窗口长度:单词个数 * 单词长度
        windows_len = len(words) * word_len
        # 给定的字符串长度
        s_len = len(s)
        ans = []

        # 以窗口的大小为单位,在字符串s中查找,i是每个窗口的起点
        for i in range(s_len - windows_len + 1):
            # 每次都要用到统计的字典,并且需要减掉个数验证是否满足题目要求
            # 所以用words_dict的浅复制,循环使用,等号是引用指向同一个内存地址
            # deepcopy是深复制,父对象独立,子对象不变,并且需要导入import copy
            temp_dict = words_dict.copy()
            # j是每个窗口一个单词的终点
            j = i + word_len
            
            # 如果s中的字符串找到了words中的一个单词,并且字典中的计数还大于0的话(表示还没用完)
            while s[j - word_len : j] in temp_dict and temp_dict[s[j - word_len : j]] > 0:
                # 当前用过的单词的计数减1
                temp_dict[s[j - word_len : j]] -= 1
                # 开始下一个单词位置
                j += word_len
            
            # 如果以i为起点并且窗口内的单词都在words_dict中,那么符合条件
            if sum(temp_dict.values()) == 0:
                ans.append(i)
        
        return ans

参考文献:
https://leetcode.cn/problems/substring-with-concatenation-of-all-words/solution/san-chong-fang-fa-zhu-jian-you-hua-by-hardcandy/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我将按照您的要求回答这个问题。首先,我们需要定义一个字符串类,它包含了字符串的基本操作。下面是一个示例实现: ```cpp #include<iostream> #include<string.h> using namespace std; class String { private: char* str; // 用char数组保存字符串 int len; // 记录字符串的长度 public: String(const char* s = "") // 构造函数 { len = strlen(s); str = new char[len + 1]; // 分配内存 strcpy(str, s); } ~String() // 析构函数 { delete[] str; // 释放内存 } int length() // 求字符串的长度 { return len; } String substr(int pos, int n) // 取一个子串 { char* s = new char[n + 1]; strncpy(s, str + pos, n); s[n] = '\0'; String sub(s); delete[] s; return sub; } int compare(String& s) // 比较两个字符串的大小 { return strcmp(str, s.str); } int find(String& sub) // 比较一个子串在主串中的位置 { char* s = strstr(str, sub.str); if (s == NULL) return -1; else return s - str; } String operator+(String& s) // 将两个子串连接成一个新的字符串 { char* s1 = new char[len + s.len + 1]; strcpy(s1, str); strcat(s1, s.str); String newstr(s1); delete[] s1; return newstr; } }; int main() { String s1("hello"); String s2("world"); String s3 = s1 + s2; int len = s1.length(); String sub = s1.substr(1, 3); int cmp = s1.compare(s2); int pos = s1.find(sub); cout << "1.求字符串的长度:" << len << endl; cout << "2.从主串中取一个子串:" << sub << endl; cout << "3.比较两个字符串的大小:" << cmp << endl; cout << "4.比较一个子串在主串中的位置:" << pos << endl; cout << "5.将两个子串连接成一个新的字符串:" << s3 << endl; cout << "0.退出系统" << endl; return 0; } ``` 程序界面如下所示: ``` 1.求字符串的长度:5 2.从主串中取一个子串:ell 3.比较两个字符串的大小:-15 4.比较一个子串在主串中的位置:1 5.将两个子串连接成一个新的字符串:helloworld 0.退出系统 ``` 该程序实现了您提出的所有功能,并且提供了一个简单的用户界面。用户可以根据需要选择执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值