알파벳이 중복되지 않은 가장 긴 문자열을 찾아라

정은경·2019년 12월 16일
0
  • 문제
    String 형인 str 인자에서 중복되지 않은 알파벳으로 이루어진 제일 긴 단어의 길이를 반환해주세요.

str: 텍스트
return: 중복되지 않은 알파벳 길이 (숫자 반환)

예를 들어,
str = "abcabcabc"
return은 3
=> 'abc' 가 제일 길기 때문

str = "aaaaa"
return은 1
=> 'a' 가 제일 길기 때문

str = "sttrg"
return은 3
=> 'trg' 가 제일 길기 때문


const getLengthOfStr = str => {
len = str.length;
temp = ""
rlt = []
max = 0

j = 1
for(let i=0; i<(len-2); i++){
temp = str[i]
//console.log(temp)
//console.log(str[j])
j=i+1

while(temp.indexOf(str[j])<0 && j<len){
  if(str[j]!=undefined){
    temp = temp + str[j]
    //console.log(temp)
    j++
  }
  //console.log(str[j])
}
rlt.push(temp)
if(max < temp.length){
  max = temp.length;
}
temp=""

}
console.log(rlt)
return max
}
getLengthOfStr("abcabc")

//console.log("abc".indexOf("d"))

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글