zoukankan      html  css  js  c++  java
  • 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    【139-Word Break(单词拆分)】


    【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

    原题

      Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
      For example, given
      s = "leetcode",
      dict = ["leet", "code"].
      Return true because "leetcode" can be segmented as "leet code".

    题目大意

      给定一个字符串s和单词字典dict,确定s能否够切割成一个或多个单词空格分隔的序列。

    解题思路

      一个字符串S,它的长度为N。假设S能够被“字典集合”(dict)中的单词拼接而成,那么所要满足的条件为:
    * F(0, N) = F(0, i) && F(i, j) && F(j, N);
    * 这样子,假设我们想知道某个子串是否可由Dict中的几个单词拼接而成就能够用这种方式得到结果(满足条件为True, 不满足条件为False)存入到一个boolean数组的相应位置上,这样子,最后boolean 数组的最后一位就是F(0, N)的值,为True表示这个字符串S可由Dict中的单词拼接,否则不行!

    代码实现

    算法实现类

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class Solution {
    
        public boolean wordBreak(String s, Set<String> wordDict) {
            // 參数校验
            if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
                return false;
            }
    
            // 标记是否匹配,match[i]表表[0, i-1]都匹配
            int length = s.length();
            boolean[] match = new boolean[length + 1];
            match[0] = true;
    
            for (int i = 1; i < length + 1; i++) {
                for (int j = 0; j < i; j++) {
                    if (match[j] && wordDict.contains(s.substring(j, i))) {
                        // f(0,n) = f(0,i) + f(i,j) + f(j,n)
                        match[i] = true;
                        break;
                    }
                }
            }
    
    
            return match[length];
        }
    
    
        // 以下是还有一种解法。可是会超时
        public boolean wordBreak2(String s, Set<String> wordDict) {
    
            // 參数校验
            if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
                return false;
            }
    
            Map<Character, Set<String>> wordMap = new HashMap<>(wordDict.size());
            // 将全部開始字符同样的单词放入一个Set中
            for (String word : wordDict) {
                Set<String> set = wordMap.get(word.charAt(0));
                if (set == null) {
                    // 新创建一个set放入Map中
                    set = new HashSet<>();
                    wordMap.put(word.charAt(0), set);
                }
                // 单词存入set中
                set.add(word);
            }
    
            return wordBreak(s, 0, wordMap);
        }
    
        /**
         * 搜索字符串能否够被切割成单词串
         *
         * @param s       字符串
         * @param idx     处理的開始位置
         * @param wordMap 单词字典,開始字符同样的在同一个set集合中
         * @return 搜索结果
         */
        public boolean wordBreak(String s, int idx, Map<Character, Set<String>> wordMap) {
    
            if (idx >= s.length()) {
                return true;
            }
    
            Set<String> words = wordMap.get(s.charAt(idx));
    
            if (words != null) {
                for (String word : words) {
                    // idx之前的字符已经匹配,假设从ide之后起匹配word单词
                    if (s.startsWith(word, idx)) {
                        // 递归处理
                        boolean result = wordBreak(s, idx + word.length(), wordMap);
                        // 假设满足条件,返回true
                        if (result) {
                            return true;
                        }
                    }
                }
            }
    
            return false;
        }
    }

    评測结果

      点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

    这里写图片描写叙述

    特别说明

    欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47774547

  • 相关阅读:
    编写JS代码的“use strict”严格模式及代码压缩知识
    开发网站要从用户的角度出发!
    你好,世界
    JavaScript的几种函数的结构形式
    JavaScript功能检测技术和函数构造
    android打造万能的适配器
    C语言第二次博客作业分支结构
    C语言第三次博客作业单层循环结构
    C语言第一次博客作业——输入输出格式
    C语言第四次博客作业嵌套循环
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7241406.html
Copyright © 2011-2022 走看看