What data structure/aglorithm to use for strings to see if there exists two strings you can concatenate to get a target? I want to do this as efficiently as possible.
I want to come up with an arbitrary target string, and feed it into my program to see if there exists two strings that I can add to get the target string. What kind of data structure and algorithm would be good for this? I'm thinking a sorted array would be good, and then walk down the list, binary searching for the suffix/prefix needed. That would be O of NlogN I think.
>>60311708
>>60311708
Are you..
Are you trying to do A7 for Hughes?
> see if there exists two strings you can concatenate to get a target?
NP complete, certificate is the two strings.
>>60312218
What is A7 and who is Hughes
>>60311708
Why a sorted array?
Why not use a HashSet to look for prefixes and then suffixes?
And lastly, why not use substrings of your target string as a vantage point for finding these prefixes, rather than iterating over a bunch of strings that may not even qualify as substrings?
>>60311708
>>60315925
This here is what I was thinkingpublic String[] ayy(HashSet<String> strPool, String target) {
for (int i = 1; i < target.length(); i++) {
//find prefix
String s1 = target.substring(0, i);
if (strPool.contains(s1) == false) {
continue;
}
//find suffix
String s2 = target.substring(i);
if (strPool.contains(s2)) {
return new String[]{s1, s2};
}
}
return null;
}
>>60311708
The brute force approach would be:
get a set of strings and a target.
Make a set S1 of all the strings in the set that are prefixes of the target.
Make a set S2 of all the strings in the set that are suffixes of the target.
Then try to find a match:
For each string in s1, search for a matching suffix in s2. You could optimize it by only looking
at those suffixes in s2 that have the right length for your candidate in s1.
I haven't implemented it, but it seems like it could be efficient enough depending on how many strings are in your set.
>60313037
>NP complete, certificate is the two strings.
That's not how you show something is NP complete.
>>60317103
You only need a single set
See >>60316756
>>60317325
Post more cats
>>60317386
I'm running out of cute non-reaction ones.
>>60311708
Make a hashmap of all the strings in your program, then take the new string, split it in two at the middle and check if each part is in the hash, if they aren't, split one character over/under and try again.
>>60317501
You would only be using the keyset of the hashmap, so just using a hashset is enough.