LeetCode 30.串联所有单词的子串

题目:

给定一个字符串 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"]

输出:[]

思路

简单的动态规划。

后面再补充讲解。

代码:

java 复制代码
public class Q0030 {

    public static void main(String[] args) {

        demo1();
        demo2();
        demo3();

    }

    private static void demo1() {
        String s = "wordgoodgoodgoodbestword";
        String[] words = {"word", "good", "best", "word"};
        List<Integer> substring = findSubstring(s, words);
        System.out.println(substring);
    }

    private static void demo2() {
        String s = "barfoothefoobarman";
        String[] words = {"foo", "bar"};
        List<Integer> substring = findSubstring(s, words);
        System.out.println(substring);
    }

    private static void demo3() {
        String s = "wordgoodgoodgoodbestword";
        String[] words = {"word", "good", "best", "good"};
        List<Integer> substring = findSubstring(s, words);
        System.out.println(substring);
    }


    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<Integer>();

        int length = words[1].length();
        // i 起始位置
        for (int i = 0; i < s.length() - length; i++) {
            List<String> wordsList = new ArrayList<>();
            for (String word : words) {
                wordsList.add(word);
            }
            StringBuffer stringBuffer = new StringBuffer();
            for (int after = 0; after < length; after++) {
                char x = s.charAt(i + after);
                stringBuffer.append(x);
            }
            String string = stringBuffer.toString();
            if (!wordsList.contains(string)) {
                continue;
            } else {
                int flag = 1;
                if (flag == 0) {
                    continue;
                }
                for (int j = i; j < s.length() - length; j += length) {
                    StringBuffer stringBuffer1 = new StringBuffer();
                    for (int after = 0; after < length; after++) {
                        char x = s.charAt(i + after);
                        stringBuffer1.append(x);
                    }
                    String string1 = stringBuffer1.toString();
                    if (wordsList.contains(string1)) {
                        wordsList.remove(string1);
                        if (wordsList.isEmpty()) {
                            flag = 0;
                            result.add(i);
                        }
                        continue;
                    } else {
                        flag = 0;
                        break;
                    }
                }
            }
        }
        return result;
    }
}

Over~

相关推荐
Emberone2 分钟前
数据结构:算法的时间复杂度和空间复杂度
数据结构·算法
cyforkk2 分钟前
16、Java 基础硬核复习:网络编程的核心逻辑与面试考点
java·网络·面试
好好学习天天向上~~2 分钟前
2_Linux学习总结_基础指令
linux·学习
脑洞代码3 分钟前
协议头部格式详解:IP、TCP、UDP与MAC帧结构
网络·笔记·学习
YuTaoShao10 分钟前
【LeetCode 每日一题】3013. 将数组分成最小总代价的子数组 II
算法·leetcode·职场和发展
serve the people12 分钟前
python环境搭建 (五) Dockerfile 和 docker-compose.yml 核心作用
java·python·docker
独断万古他化19 分钟前
【Spring 事务】核心概念与实战:从手动控制到注解自动事务
java·spring·事务
爱尔兰极光20 分钟前
LeetCode 热题 100--字母异位词分组
算法·leetcode·职场和发展
要开心吖ZSH21 分钟前
Spring AI Alibaba 个人学习笔记
人工智能·学习·spring·spring ai·springaialibaba
马猴烧酒.21 分钟前
【团队空间|第十一天】基础功能实现,RBAC权限控制,ShardingSphere详解
java·开发语言·数据库