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
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.
There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.
Whenever a change to a String is made, an entirely new String is created. Concatenation is the process of joining end-to-end.
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".
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.
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 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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With