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~

相关推荐
8Qi814 分钟前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
十月的皮皮1 小时前
C语言学习笔记20260606- 求月份天数三种写法
c语言·笔记·学习
马士兵教育1 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
youngerwang1 小时前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】
网络·算法·架构·芯片
snow@li2 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
KaMeidebaby2 小时前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习
云烟成雨TD2 小时前
Spring AI 1.x 系列【57】动态工具发现:Tool Search Tool
java·人工智能·spring
zfoo-framework2 小时前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
lizhihai_992 小时前
股市学习心得-AI 产业链核心标的梳理清单
大数据·服务器·人工智能·科技·学习