zoukankan      html  css  js  c++  java
  • Leetcode: Concatenated Words

    Given a list of words, please write a program that returns all concatenated words in the given list of words.
    
    A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
    
    Example:
    Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
    
    Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
    
    Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; 
     "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; 
    "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
    Note:
    The number of elements of the given array will not exceed 10,000
    The length sum of elements in the given array will not exceed 600,000.
    All the input string will only include lower case letters.
    The returned elements order does not matter.

    Scan through array and DP:

    We iterate through each word and see if it can be formed by using other words. The subproblem is Word Break I.

    But it is a little different from Word Break I, because our result only want words that are concantenated by 2 or more other words in the given array.

    How to tell if a word is only by itself in the given array, or it can be concatenated by other words in the given array?

    The trick is: it is obvious that a word can only be formed by words shorter than it. So we can first sort the input by length of each word, and only try to form one word by using words in front of it. We also do not add the current word to dictionary when determine if it can be concantenated.

    小心一个test case:

    Input:[""]
    Output:[""]
    Expected:[]
    如果不加21行那句,就会直接return dp[0], true了
     1 public class Solution {
     2     public List<String> findAllConcatenatedWordsInADict(String[] words) {
     3         List<String> res = new ArrayList<>();
     4         if (words==null || words.length==0) return res;
     5         HashSet<String> dict = new HashSet<>();
     6         Arrays.sort(words, new Comparator<String>() {
     7             public int compare(String str1, String str2) {
     8                 return str1.length() - str2.length();
     9             }
    10         });
    11 
    12         for (String word : words) {
    13             if (canForm(word, dict))
    14                 res.add(word);
    15             dict.add(word);
    16         }
    17         return res;
    18     }
    19     
    20     public boolean canForm(String word, HashSet<String> dict) {
    21         if (dict.isEmpty()) return false;
    22         boolean[] dp = new boolean[word.length()+1];
    23         dp[0] = true;
    24         for (int i=1; i<=word.length(); i++) {
    25             for (int j=0; j<i; j++) {
    26                 if (!dp[j]) continue;
    27                 String str = word.substring(j, i);
    28                 if (dp[j] && dict.contains(str)) {
    29                     dp[i] = true;
    30                     break;
    31                 }
    32             }
    33         }
    34         return dp[word.length()];
    35     }
    36 }

    这题还应该研究一下Trie的解法, 目前还没有想好,也没有看到不错的Trie 解法

  • 相关阅读:
    PS做图片,如何使背景透明
    C# 在线培训之零基础入门 01:开篇及C#程序、解决方案的结构
    C# 在线培训之零基础入门 02:源码管理之TFS入门
    [ASP.NET] 浅析HtmlForm控件
    Asp.net内置对象之Session
    Asp.net内置对象之Cookies
    [WinForm] 自动补全控件
    ASP.NET内置对象之Request对象
    C#开发Activex控件与JavaScript的互调
    XHTML学习资料(五)—— 表单
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6213426.html
Copyright © 2011-2022 走看看