bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1180. Count Substrings with Only One Distinct Letter

1180. Count Substrings with Only One Distinct Letter

Leetcode Solutions

Arithmetic Sequence Approach

  1. Initialize total to 0 to count the number of substrings.
  2. Initialize two pointers left and right to 0. These pointers define the boundaries of the current segment with one distinct letter.
  3. Iterate through the string s using the right pointer.
  4. If right has not reached the end of the string and s[right] is the same as s[left], increment right to extend the current segment.
  5. If s[right] is different from s[left], calculate the length of the current segment as right - left, apply the arithmetic sequence sum formula to get the number of substrings for this segment, and add it to total.
  6. Set left to the current right position to start a new segment.
  7. Repeat steps 4-6 until right reaches the end of the string.
  8. After the loop, ensure to add the substrings count for the last segment.
UML Thumbnail

Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...