def num_repeats(string)
letters = string.chars
idx = 0
n = 1
arr = []
lettercount = 0
while idx < letters.length
lettercount = 0
while n < letters.length
if letters[idx] == letters[n]
lettercount = 1
end
n+=1
end
if lettercount > 0
arr.push(idx)
end
idx += 1
end
return arr.length
end
puts(num_repeats("abdbccc"))
# == 2 since 2 letters are repeated across the string of characters
我一直收到0,雖然我看到它,如果重複一個數字,numbercount的值應該從零移到一個,然後允許一些值被推入數組,然後我得到所述數組的長度確定重複字符的數量。我的循環有問題嗎?如何獲得某個字符串中重複的所有字符的數值?
我不明白你想要做什麼。也許發佈一些預期的輸入/輸出? – Adrian
我希望這個評論澄清一點 – Chris