Special String Again

Solving the Programming Challenge
Difficulty: Medium Source: Hacker Rank
Feature Image

Problem

A string is said to be a special string if either of two conditions is met:

All of the characters are the same, e.g. aaa. All characters except the middle one are the same, e.g. aadaa. A special substring is any substring of a string which meets one of those criteria. Given a string, determine how many special substrings can be formed from it.

For example, given the string s= mnonopoo, we have the following special substrings:

{m, n, o, n, o, p, o, o, non, ono, opo, oo}

Read more on the challenge page…


Problem Analysis and Solution


My Solution

I’m providing the solution for Python and JS, please leave on the comments if you found a better way.