String algorithm suggestion to find all the common prefixes of a list of strings -



String algorithm suggestion to find all the common prefixes of a list of strings -

what algorithm suggest find out longest mutual prefixes of list of strings?

i might have strings such as:

call mike , schedule meeting. phone call lisa phone call adam , inquire quote. implement new class iphone project implement new class rails controller purchase groceries

i want find out next prefixes:

"call " "implement new class "

i'll using objective c, ready made cocoa solution plus (though not must).

edit: clarified question:

sort strings find longest mutual prefix of each adjacent pair sort , dedupe mutual prefixes, remove that's strict prefix of another.

actually, step (3) requires remove that's dupe/prefix of another, trie or whatever instead of sorting. in fact may whole thing can done faster suitably annotated trie - if include "count" @ each node you're looking exactly nodes count of 2+, have no children count of 2+.

but sorting built in, , 1 time you've sorted can observe prefixes looking @ adjacent items, it's less effort.

[original answer:

just one-off operation, find longest mutual prefix between strings?

i'd in terms of length of prefix. in pseudo-code, , assuming nul-terminated strings:

prefixlen = strlen(first_string); foreach string in list { (i = 0; < prefixlen; ++i) { if (string[i] != first_string[i]) { prefixlen = i; break; } } if (prefixlen == 0) break; } common_prefix = substring(firststring, 0, prefixlen);

]

string algorithm search

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -