Skip to content
Advertisement

Find how much of a string is needed before it becomes unique among a set of strings

This is difficult to explain, but I’ll do my best…

I have an array of strings. Let’s use an example here:

var myArray = [
    "hello there my friend robert"
    "hello there friend kimberly"
    "hello friend claire"
    "hi there friend chris"
]

I’m trying to write a function that will determine how many words of each string in the array are needed before it becomes unique. For example, using the above array as input, the function should return the following:

[
    "hello there my",
    "hello there friend",
    "hello friend",
    "hi"
]

The strings in the returned array represent the words needed for each string before the string becomes unique.

For instance, the function returns “hello there my” for “hello there my friend robert.” This is because “hello there” on its own is ambiguous. It could refer to either “hello there my friend robert” or “hello there friend kimberly.” However, “hello there my” could only refer to “hello there my friend robert.”

I’m totally at a loss for how to accomplish this. Does anyone else know of a solution?

(For simplicity, assume the input strings have no punctuation and are entirely lowercase.)

Advertisement

Answer

I think I have some idea regarding it. What you can do is store the list like.

{
   'first' : ['hello', 'hello', 'hello', 'hi'],
   'second' : ['there','friend','there'],
   'third' : ['my', 'friend', 'claire'],
   'fourth' : ['robert', 'kimberly', 'chris']
}

now just check if the count of the first is more than one on this map then you have to go ahead and if the count is one, you have to stop there.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement