Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if strings in a list can be formed by concatenation of elements in the same list

Check if strings in a list can be formed by concatenation of elements in the same list

For example:

Input List -

{ best,  rockstar,   star,  guide,  bestguide, rock }

Output :-

rockstar -> rock, star

bestguide -> best, guide

Here "rockstar" can be formed using rock and star. Similarly "bestguide" can be formed by joining "best" and "guide".

Solution so far I have- Create all the combinations of string by joining each other(2 string together, 3 string together and so on) and store in a Map.

map structure could be as following

Map<String, List<String>>

{rockstar : [rock, star], ....}

Now check just traverse original list and check in the map. If it's found then it's one of possible solution.

Looking for a better solution with better time/space complexity

like image 838
nagendra547 Avatar asked Nov 08 '19 02:11

nagendra547


People also ask

How do you concatenate a list of elements in a string?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

What is difference between string and concatenation?

Whenever a change to a String is made, an entirely new String is created. Concatenation is the process of joining end-to-end.

Which is an example of string concatenation?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".

What are concatenated words in a list?

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.

Can STR1 be formed by concatenating the string STR2 repetitively?

Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not. Concatenating the string str2 thrice generates the string (“abc” + “abc” + “abc” = ) “abcabcabc”. Therefore, the required output is Yes.

How can we know if S is a concatenation of L?

How can we know if S is a one of all the possible concatenations of L? S is "abcd" + "a" + "bc" + "e", then S is a concatenation of L, whereas "ababcecd" is not.

What is the dynamic programming approach for string concatenation?

A dynamic programming approach would be to work left to right, building up an array A [x] where A [x] is true if the first x characters of the string form one of the possible concatenations of L.


3 Answers

I think one standard approach would probably be to construct a trie from the dictionary. Then for each candidate, walk the trie and when a matching path reaches the end (marking a smaller word), continue from the top of the trie again with the remaining suffix of the candidate. We may need a few backtracking trials per candidate if similar matches exist; but in a dictionary of only 10,000, unless the data is degenerate, these should hopefully be few on average.

like image 150
גלעד ברקן Avatar answered Oct 23 '22 18:10

גלעד ברקן


First, sorry for my bad English. I have a naive way, you should try it:

step 1: Sort the list in descending order of lengths of elements

step 2: In turn (from left to right of sorted list), add elements one by one to a tree under following rules:

  • Each node of the tree contains a string, the root of the tree contains nothing

  • String in each parent node contains strings in its child nodes.

enter image description here

  • step 3: Get results: if length of string in a node equals sum of lengths of strings in child nodes then we get a desired result.
like image 41
TaQuangTu Avatar answered Oct 23 '22 16:10

TaQuangTu


Here is the brute force approach. We can first form a list of the original terms, and then double-iterate that list to generate all combination possibilities. For each combination which is also already contained within the original list, we print that combination to the console.

String[] terms = new String[] { "best",  "rockstar",   "star",  "guide",  "bestguide", "rock" };
List<String> list = Arrays.asList(terms);
Set<String> set = new HashSet<String>(list);
for (int i=0; i < list.size()-1; ++i) {
    for (int j=i+1; j < list.size(); ++j) {
        if (set.contains(list.get(i) + list.get(j))) {
            System.out.println(list.get(i) + list.get(j) + " -> " + list.get(i) + ", " + list.get(j));
        }
        if (set.contains(list.get(j) + list.get(i))) {
            System.out.println(list.get(j) + list.get(i) + " -> " + list.get(j) + ", " + list.get(i));
        }
    }
}

This prints:

bestguide -> best, guide
rockstar -> rock, star
like image 38
Tim Biegeleisen Avatar answered Oct 23 '22 18:10

Tim Biegeleisen