zoukankan      html  css  js  c++  java
  • LeetCode 472. Concatenated Words

    原题链接在这里:https://leetcode.com/problems/concatenated-words/

    题目:

    Given a list of words (without duplicates), 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:

    1. The number of elements of the given array will not exceed 10,000
    2. The length sum of elements in the given array will not exceed 600,000.
    3. All the input string will only include lower case letters.
    4. The returned elements order does not matter.

    题解:

    For each word, it could be composed by shorter words.

    Sort the words with length, then for each word check if it could be composed by previous words.

    Time Complexity: O(m*logm + m*n^2). m = words.length. n = longest word length.

    Space: O(m). 

    AC Java:

     1 class Solution {
     2     public List<String> findAllConcatenatedWordsInADict(String[] words) {
     3         List<String> res = new ArrayList<>();
     4         if(words == null || words.length == 0){
     5             return res;
     6         }
     7         
     8         Arrays.sort(words, (a, b) -> a.length() - b.length());
     9         HashSet<String> hs = new HashSet<>();
    10         
    11         for(int i = 1; i < words.length; i++){
    12             hs.add(words[i - 1]);
    13             if(couldCompose(words[i], hs)){
    14                 res.add(words[i]);
    15             }
    16         }
    17         
    18         return res;
    19     }
    20     
    21     private boolean couldCompose(String s, Set<String> hs){
    22         if(s == null || s.length() == 0 || hs == null || hs.size() == 0){
    23             return false;
    24         }
    25         
    26         int n = s.length();
    27         boolean [] dp = new boolean[n + 1];
    28         dp[0] = true;
    29         for(int i = 1; i <= n; i++){
    30             for(int j = 0; j < i; j++){
    31                 if(dp[j] && hs.contains(s.substring(j, i))){
    32                     dp[i] = true;
    33                     break;
    34                 }
    35             }
    36         }
    37         
    38         return dp[n];
    39     }
    40     
    41 }

    类似Word Break.

  • 相关阅读:
    codeforces 669C C. Little Artem and Matrix(水题)
    codeforces 669B B. Little Artem and Grasshopper(水题)
    oracle drop table recyclebin恢复
    odu恢复drop表--不通过logmnr挖掘object_id
    odu恢复drop表--通过logmnr挖掘object_id
    odu恢复delete 表
    GO学习-(7) Go语言基础之流程控制
    GO学习-(6) Go语言基础之运算符
    GO学习-(4) Go语言基础之变量和常量
    GO学习-(3) VS Code配置Go语言开发环境
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12185505.html
Copyright © 2011-2022 走看看