Similar Problems

Similar Problems not available

Count Ways To Build Good Strings - Leetcode Solution

Companies:

LeetCode:  Count Ways To Build Good Strings Leetcode Solution

Difficulty: Medium

Topics: dynamic-programming  

Problem:

Given a string s, we call (i, j) an interval good if the number of distinct characters in s[i, j] is the same as the length of the interval. Return the number of good intervals in s.

Example 1:

Input: s = "xyzzaz" Output: 1 Explanation: There are 4 good intervals: [1,3], [2,4], [3,5] and [4,6]. Only [2,4] is a good interval, since the unique characters in s[2,4] are "z" and "a" and the interval has length 3. Example 2:

Input: s = "aabca" Output: 2 Explanation: There are 3 good intervals: [1,2], [2,3] and [3,4]. Only [1,2] and [3,4] are good intervals. Example 3:

Input: s = "aabcacb" Output: 3 Explanation: There are 6 good intervals: [1,2], [2,3], [3,4], [1,4], [2,5] and [3,6]. Only [1,4], [2,5] and [3,6] are good intervals.

Solution:

Approach:

To count the number of intervals that satisfy the given condition, we can traverse through the string s. For each index i from 0 to n – 1, we can calculate the length of the longest subarray that satisfies the condition and ends at index i, and the number of such subarrays.

The length of the longest subarray that satisfies the condition and ends at index i can be calculated using a sliding window approach. We can maintain a set of characters that have appeared in the current subarray and a pointer j that initially points to index i. We can then move the pointer j to the left until the length of the subarray is the same as the number of distinct characters in it. The length of the subarray is then i – j + 1.

We can then calculate the number of subarrays that end at index i and have length equal to the length of the longest subarray that satisfies the condition and ends at index i. This can be done using the formula (length + 1) * length / 2, where length is the length of the longest subarray that satisfies the condition and ends at index i.

The total number of good intervals is the sum of the number of subarrays that satisfy the condition and end at each index i.

Algorithm:

  1. Initialize a variable res to 0.
  2. Initialize a set chars to an empty set.
  3. Initialize a variable len to 0.
  4. Initialize a variable j to i.
  5. Traverse through the string s from index i to n – 1.
  6. If s[j] is not in chars, add it to chars and increment len.
  7. If the length of chars is not equal to len, remove s[j] from chars and decrement len, and move the pointer j to the left.
  8. If the length of chars is equal to len, calculate the length of the subarray (i – j + 1) and store it in a variable length.
  9. Calculate the number of subarrays that end at index i and have length equal to length using the formula (length + 1) * length / 2, and add it to res.
  10. Return res.

Time Complexity:

We traverse through the string s once, and for each index i, we traverse through s from index i to n – 1. Therefore, the time complexity is O(n^2).

Space Complexity:

We use a set chars to store the distinct characters in the current subarray, and three integer variables res, len and j. Therefore, the space complexity is O(1).

Python Code:

Count Ways To Build Good Strings Solution Code

1