문제 링크 : Longest Common Prefix
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let shortStr = strs[0]
strs.forEach(el => {
if(shortStr.length > el.length){
shortStr = el
}
})
let result = "";
shortStr = shortStr.split('')
let chk = 0
shortStr.forEach(e => {
strs.forEach(el => {
if(el.includes(e)){
chk ++
}
})
if(strs.length === chk) result += e
chk = 0
})
return result
};
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let shortStr = strs[0]
strs.forEach(el => {
if(shortStr.length > el.length){
shortStr = el
}
})
let result = [];
shortStr = shortStr.split('')
let chk = 0
shortStr.forEach((e,i) => {
strs.forEach(el => {
if(el[i] === e){
chk ++
}
})
strs.length === chk ? result.push(e) : result.push('')
chk = 0
})
let answer = ""
for(let i=0; i<result.length; i++){
if(result[i] === '') break;
answer += result[i]
}
return answer
};