Longest run
Inputs
text is 0 to 20 lowercase letters (a to z).
Your tasks
- Create
bestandcurrent, both 0. - Use a loop over the positions of
text. If the character matches the one just before it, add 1 tocurrent; otherwise setcurrentto 1. - After each step, update
bestifcurrentis larger. - Complete
longestRunso it returnsbest. Return it, do not print it.
Example
With text = "aabbbcaa", longestRun returns 3.
Rules
- An empty text has a longest run of 0.
Test data
TEST DATA 1: text is "aabbbcaa"
TEST DATA 2: text is ""
Practice focus
Text is just a list of characters, so everything you know about lists applies. String problems in interviews are array problems with letters.