Given a list strings, find longest common prefix in list of strings
Arrange the words in alphabetical order. Choose the first and last words from the sorted list. Start from the beginning of both words, compare the characters at the same position in each word until the characters differ. You reach the end of the shorter word.
ls = ['work', 'words', 'word', 'wordsforwords'] ls.sort() first = ls[0] last = ls[-1] i = 0 while i < min(len(first), len(last)): if first[i] != last[i]: break i += 1 print(first[0:i])
const data = ['work', 'words', 'word', 'wordsforwords']; let output = data.sort(); const first = output[0]; const last = output[output.length - 1]; let i; for(i = 0; i