
😎풀이
words
를 순회하며 모든 문자 분해
- 첫 단어 순회
2-1. 모든 언어에 해당 문자열이 있는지 확인
2-2. 해당 문자열 정답 배열에 추가
2-3. 모든 단어로부터 해당 문자열 제거
- 공통된 문자열 반환
function commonChars(words: string[]): string[] {
const result = []
const hashMap = new Map<string, string[]>()
for(const word of words) hashMap.set(word, [...word])
const first = words[0]
for(const char of first) {
const isAllWordsIncluded = [...hashMap].every(([key, value]) => value.indexOf(char) >= 0)
if(!isAllWordsIncluded) continue
result.push(char)
for(const [key, value] of hashMap) {
const charIdx = value.indexOf(char)
const filteredValue = value.toSpliced(charIdx, 1)
hashMap.set(key, filteredValue)
}
}
return result
};