Skip to content
Advertisement

Count number of letters until one letters has changed

I want to count number of same letter at beginning between two words (letter by letter) until there’s one different and return who has the most same letter.

This is my code :

JavaScript

But i get always this error of string index out of range, I’ve tried with much way but nothing

JavaScript

In input :

JavaScript

Output:

JavaScript

Thanks

Advertisement

Answer

you can’t go out of range in your while verify the length of your word before word[letter] == i[letter] while letter < len(word) and letter < len(i) and word[letter] == i[letter]:

gives you :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement