Created
January 30, 2022 12:41
-
-
Save dividead/30b539dd3d046d1ba642af01073c0989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let list = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"].map(s => s.toLowerCase()); | |
let m = new Map() | |
let skip = new Set() | |
for (let word of list) { | |
let s = new Set() | |
m.set(word, s) | |
for (let start = 0; start < word.length; start++) { | |
for (let end = start + 1; end <= word.length; end++) { | |
s.add(word.substring(start, end)) | |
} | |
} | |
} | |
for (let word of list) { | |
let set = m.get(word) | |
for (let [key, value] of m) { | |
if (key === word) continue | |
for (let str of value) { | |
if (set.has(str)) skip.add(str) | |
} | |
} | |
} | |
let answer = [] | |
for (let word of list) { | |
let x = word | |
for (let str of m.get(word)) { | |
if (skip.has(str)) continue | |
if (str.length < x.length) x = str | |
} | |
answer.push(x) | |
} | |
console.log(answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment