Leetcode Problem 1525. Number of Good Ways to Split a String

1525. Number of Good Ways to Split a String

Leetcode Solutions

Two-Pass Solution with Prefix and Suffix Count

  1. Initialize two arrays prefix and suffix of length equal to the string length.
  2. Initialize a set uniqueChars to keep track of unique characters.
  3. Iterate over the string from left to right: a. Add the current character to uniqueChars. b. Update prefix[i] with the size of uniqueChars.
  4. Clear uniqueChars for the next iteration.
  5. Iterate over the string from right to left: a. Add the current character to uniqueChars. b. Update suffix[i] with the size of uniqueChars.
  6. Initialize a variable goodSplits to count the number of good splits.
  7. Iterate over the string, excluding the last character: a. If prefix[i] is equal to suffix[i + 1], increment goodSplits.
  8. Return goodSplits.
UML Thumbnail

HashSet and HashMap Approach

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...