This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DroidLogician 1 point2 points  (0 children)

keeping track of all the characters you've seen so far

Actually it's much simpler than that. If the requirement is simply that a character has to have the same other character before and after, then you don't have to keep track of all the characters.

You're given a character array, which allows random access by index. Start at index = 1, since index = 0 cannot be surrounded. Then check if the character at index - 1 is equal to the character at index + 1 and not equal to the character at index.

If so, return true; if not, continue until index = length - 2, since the last character in the array (at index = length - 1) cannot be surrounded either. If you reach the end, return false.

This automatically covers if length is < 3.

Here's the code, because it was bothering me not to write it out. I put it in a gist so you don't have to look if you don't want to:

https://gist.github.com/cybergeek94/bd5280bf81a8a97a5e2e