Similar Problems

Similar Problems not available

Number Of Good Ways To Split A String - Leetcode Solution

Companies:

LeetCode:  Number Of Good Ways To Split A String Leetcode Solution

Difficulty: Medium

Topics: string dynamic-programming bit-manipulation  

Problem Statement:

Given a string s, return the number of ways to split s such that each substring is a "good" substring.

A "good" substring is a substring which contains the same number of occurrences of two distinct characters.

Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 'a' and 0 'b'. ("aa", "caba") Left string and right string contains 2 'a' and 0 'b'. ("aac", "aba") Left string and right string contains 1 'a' and 1 'b'. ("aaca", "ba") Left string and right string contains 2 'a' and 1 'b'. ("aacab", "a") Left string and right string contains 2 'a' and 2 'b'.

Example 2: Input: s = "abcd" Output: 0 Explanation: There are 3 ways to split "abcd" but none of them are good.

Approach:

We can solve this problem by iterating through every possible position to split the string s and checking whether the resulting substrings are "good" or not. To check whether a substring is "good" or not, we can iterate through the characters of the substring and keep track of the number of occurrences of each character. If there are exactly two characters and both of them occur the same number of times, then the substring is "good". We can then increment a counter that keeps track of the number of "good" substrings found.

Algorithm:

  1. Initialize a counter variable to zero to keep track of the number of "good" substrings found.
  2. Iterate through every possible position to split the string s. a. Extract the left and right substrings using slicing. b. Initialize a dictionary to keep track of the number of occurrences of each character in the substring. c. Iterate through the characters of the substring and increment the corresponding count in the dictionary. d. If there are exactly two characters and both of them occur the same number of times, then the substring is "good". Increment the counter variable.
  3. Return the counter variable.

Code:

Here's the Python code to implement the above algorithm:

class Solution: def countGoodSubstrings(self, s: str) -> int: count = 0 for i in range(len(s) - 2): left = s[i:i+3] freq = {} for c in left: freq[c] = freq.get(c, 0) + 1 if len(freq) == 2 and all(v == 1 for v in freq.values()): count += 1 return count

Time Complexity:

The time complexity of the above algorithm is O(n^2) where n is the length of the string s. This is because we're iterating through every possible position to split the string and then iterating through every character in each substring.

Space Complexity:

The space complexity of the above algorithm is O(1) because we're only using a constant amount of extra space to keep track of the counter variable and the frequency of characters in each substring.

Number Of Good Ways To Split A String Solution Code

1