Skip to content

Instantly share code, notes, and snippets.

@dividead
Created January 30, 2022 12:41
Show Gist options
  • Save dividead/30b539dd3d046d1ba642af01073c0989 to your computer and use it in GitHub Desktop.
Save dividead/30b539dd3d046d1ba642af01073c0989 to your computer and use it in GitHub Desktop.
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