LeetCode practice 472: connectives

Posted by opencombatclan on Tue, 21 Dec 2021 20:26:23 +0100

I won't say much about the importance of algorithms. If you want to go to a large factory, you must go through the interview of basic knowledge and business logic + algorithm. So, in order to improve the algorithm ability, this official account will be followed up every day to do an algorithm question, and the topic will be selected from LeetCode.

The question we are talking about today is called conjunctions. Let's look at the question first:

https://leetcode-cn.com/problems/concatenated-words/

Given an array of strings words (without duplicates), return all the 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.

Give you a string array words without repeated words. Please find and return all conjunctions in words.

A conjunction is defined as a string consisting entirely of at least two shorter words in a given array.

Example

Example 1:

Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output:["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation:"catsdogcats" from "cats", "dog" and "cats" form; 
     "dogcatsdog" from "dog", "cats" and "dog" form; 
     "ratcatdogcat" from "rat", "cat", "dog" and "cat" form.

Example 2:

Input: words = ["cat","dog","catdog"]
Output:["catdog"]

Problem solving

https://www.cnblogs.com/kexinxin/p/10280230.html

For each word w in words, we define an array dp[n+1]. If dp[i] == true, it means that w.substr(0, i) can be connected by existing words in words. Then the state transition equation is: DP [i] = {DP [J] & & W. substr (j + 1, I - J) is in words}, where j < I. Finally, check whether dp[n] is true. If so, add it to the result set. In order to speed up the search for words in words, we use a hash table to save each word. In this way, the time complexity can be reduced to O(n * m^2), where n is the number of words in words and M is the average length (or maximum length?) of each word.

import java.util.*;

class Solution {
    public List<String> findAllConcatenatedWordsInADict(String[] words) {
        Set<String> set=new HashSet<String>();
        for(int i=0;i<words.length;i++){
            set.add(words[i]);
        }
        List<String> res=new ArrayList<String>();
        for(String word:words){
            int n=word.length();
            boolean[] dp=new boolean[n+1];
            Arrays.fill(dp,false);
            dp[0]=true;
            for(int i=0;i<n;i++){
                if(!dp[i]) continue;
                for(int j=i+1;j<=n;j++){
                    if(j-i<n && set.contains(word.substring(i,j))){
                        dp[j]=true;
                    }
                }
                if(dp[n]){
                    res.add(word);
                    break;
                }
            }
        }
        return res;
    }
}

Well, that's all for today's article. If you feel you have something to gain, please click to read or forward it. Your support is my greatest motivation.

Last tweet:

Leetcode 1-460 question summary, I hope it will be helpful to you!

LeetCode practice 461: Hamming distance

LeetCode practice 462: make the array elements equal with the minimum number of moves II

LeetCode practice 463: the perimeter of the island

LeetCode practice 464: can I win

LeetCode practice 465: optimal bill balance

LeetCode practice 466: count the number of repetitions

LeetCode practice 467: the only substring in the surrounding string

LeetCode practice 468: verifying the IP address

LeetCode practice 469: convex polygon

LeetCode problem brushing practice 470: Rand10() is implemented with Rand7()

LeetCode practice 471: encoding the shortest length string