# Solving Leetcode Interviews in Seconds with AI: Number of Good Ways to Split a String


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1525" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> You are given a string s. A split is called good if you can split s into two non-empty strings sleft and sright where their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters in sleft and sright is the same. Return the number of good splits you can make in s.   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 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.  Example 2:  Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd").    Constraints:  1 <= s.length <= 105 s consists of only lowercase English letters.  

	# Explanation
	Here's the solution:

*   **High-Level Approach:**
    *   Precompute the number of distinct characters to the left and right for each possible split point using prefix and suffix arrays.
    *   Iterate through the string, comparing the number of distinct characters in the left and right substrings at each split point.
    *   Increment a counter for each good split found.

*   **Complexity:**
    *   Runtime: O(n)
    *   Storage: O(n)

	
	# Code
	```python
	def num_good_splits(s: str) -> int:
    n = len(s)
    left_distinct = [0] * n
    right_distinct = [0] * n

    distinct_chars = set()
    for i in range(n):
        distinct_chars.add(s[i])
        left_distinct[i] = len(distinct_chars)

    distinct_chars = set()
    for i in range(n - 1, -1, -1):
        distinct_chars.add(s[i])
        right_distinct[i] = len(distinct_chars)

    count = 0
    for i in range(n - 1):
        if left_distinct[i] == right_distinct[i + 1]:
            count += 1

    return count
	```
			
