535. Count Substrings with Only One Distinct Letter

Given a string s, return the number of substrings that have only one distinct letter.

Example 1:

Input: s = "cccdc"

Output: 8

Explanation: The substrings with one distinct letter are:

  • "ccc" → 1 occurrences
  • "cc" → 2 occurrences
  • "c" → 4 occurrences
  • "d" → 1 occurrence
  • "cdc" and "cd" are not valid since they contain more than one distinct letter.

Total: 1 + 2 + 4 + 1 = 8.

Example 2:

Input: s = "mmmmnmm"

Output: 14

Explanation: The substrings with one distinct letter are:

  • "mmmm" → 1 occurrences
  • "mmm" → 2 occurrences
  • "mm" → 4 occurrences
  • "m" → 6 occurrences
  • "n" → 1 occurrence

Total: 1 + 2 + 4 + 6 + 1 = 14.

Now Your Turn!

Pick the correct output for the given input

Input: s = "tttttt"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 1000
  • s[i] consists of only lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int countLetters(string S) {
// Your code goes here
}
};
Test Case

Input:

S