美文网首页
字符串和单词字典

字符串和单词字典

作者: Djbfifjd | 来源:发表于2021-01-08 19:45 被阅读0次

一、问题描述

给定一个字符串 source 和一个单词字典,确定 source 是否可以被字典分解为多个单词。如:
给定字符串:source=“HelloWorld”。单词字典:dict=[“Hello”,“World”]。由于“HelloWorld”可被分割为“Hello”,“World”,所以返回 true。

二、解法一

遍历 dict 中的单词,查看其是否在 source 的起始位置,若在则继续查看 source 剩下的部分,否则返回 false。

public static boolean wordBreak(String source, Set<String> dict, int start) {
    if (start == source.length())
        return true;
    for (String a : dict) {
        int len = a.length();
        int end = len + start;
        //长度大于source,说明不可能是dict中的这个单词,遍历尝试dict中下一个单词
        if (end > source.length())
            continue;

        if (source.substring(start, end).equals(a))
            if (wordBreak(source, dict, end))
                return true;
    }
    return false;
}

三、解法二

利用一个长度为source.length+1的 boolean 型数组 t,初始化时将 t[0] 设为 true,其它设为 false。t[i] == true表示0~(i-1)可被分割,最后可以通过查看t[s.length()]的值判断 source 是否能被 dict 分割。

public static boolean wordBreak(String s, Set<String> dict) {
    boolean[] t = new boolean[s.length() + 1];
    t[0] = true;

    for (int i = 0; i < s.length(); i++) {
        if (!t[i])
            continue;

        for (String a : dict) {
            int len = a.length();
            int end = i + len;
            if (end > s.length())
                continue;

            if (t[end]) continue;

            if (s.substring(i, end).equals(a)) {
                t[end] = true;
            }
        }
    }
    return t[s.length()];
}

四、解法三

使用正则表达式

public static void breakWord(HashSet<String> dict) {
    StringBuilder sb = new StringBuilder();
    for (String s : dict) {
        sb.append(s + "|");
    }
    String pattern = sb.substring(0, sb.length() - 1);
    pattern = "(" + pattern + ")*";
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher("HelloWorld");

    if (m.matches()) {
        System.out.println("match");
    }
}

五、测试主类

public static void main(String[] args) {
    String source = "HelloWorld";
    Set<String> dict = new HashSet<>();
    dict.add("Hello");
    dict.add("World");
    System.out.println(wordBreak(source, dict, 0));
}

相关文章

  • 字符串和单词字典

    一、问题描述 给定一个字符串 source 和一个单词字典,确定 source 是否可以被字典分解为多个单词。如:...

  • 力扣 140 单词拆分 II

    题意:给定一个字符串和一个字典,返回可以用字典拼接的所有字符串 思路:用set记录字典的单词,用list arra...

  • 每日一题之单词拆分II

    题目140:单词拆分II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空...

  • iOS 字典与字符串互相转换

    Swift: Swift3 JSON字符串和字典互转(JSON字符串转字典和字典转JSON字符串) http://...

  • number-of-matching-subsequences

    给定一个字符串S 和一个单词字典 words,问, words中一共有多少个单词words[i]是字符串S的子序列...

  • 139. 单词拆分

    给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s ...

  • 139:单词拆分

    题意 给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出...

  • LeetCode 139. 单词拆分

    题目 给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出...

  • 算法面试

    1、深拷贝 2、给你一个字符串s和一个字符串列表wordDict作为字典,请你判断是否可以利用字典中出现的单词拼接...

  • 力扣 212 单词搜索 II

    题意:给定一个字典和一个字符矩阵,在矩阵找出所有字典中的字符串 思路: 用trie把字典中的单词加入trie中 遍...

网友评论

      本文标题:字符串和单词字典

      本文链接:https://www.haomeiwen.com/subject/dhmzoktx.html