Hints for SRM 605, D2, 250-pointer (AllenAndPassword)

James S. Plank

Wed Jan 29 10:56:08 EST 2014
Problem Statement.
Please try to do this one without my help. If you need my help, look below.
If a problem like this confuses you, start looking through the examples, and see if that gives you insight. Examples 0 and 1 and straightforward, and they may lead you to believe that you should just return s.size(). However, Example 2's size is greater than seven, and they don't give you any help. So work through it. Let's list all of the passwords that you can generate by deleting each letter in order. I'll color the letters before the one deleted red, and the letters afterward blue:

ABACCCCABAA
ABACCCCABAA
AAACCCCABAA
AABCCCCABAA
AABACCCABAA
AABACCCABAA
AABACCCABAA
AABACCCABAA
AABACCCCBAA
AABACCCCAAA
AABACCCCABA
AABACCCCABA

It should be pretty clear that the first two equal each other, because of those adjacent A's. In fact, every time there are consecutive letters that are the same, you get identical passwords.

So, formalize that: Each group of consecutive, identical letters results in one password. So, we should count the groups of identical, consecutive letters. In Example 2, those groups are "AA", "B", "A", "CCCC", "A", "B", and "AA". There are seven of them.

Can you figure out how to count groups of identical, consecutive letters? Try it before you read on.


Run through the string with an integer i. If i is zero, then its letter starts a group of consecutive letters. Otherwise, if S[i] does not equal S[i-1], then its letter starts a new group of consecutive letters.

That should give you all of the information that you need to solve this problem. Go for it!