coding-interview|October 04, 2020|2 min read

Leetcode - Split a String Into the Max Number of Unique Substrings

TL;DR

Backtracking — try every possible prefix as the next substring, add it to a set if unique, recurse on the remainder. Track the maximum count of unique splits across all paths.

Leetcode - Split a String Into the Max Number of Unique Substrings

Problem Statement

Given a string s, return the maximum number of unique substrings that the given string can be split into.

You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.

A substring is a contiguous sequence of characters within a string.

Example

Input: s = "ababccc"
Output: 5
Explanation: ['a', 'b', 'ab', 'c', 'cc']

Input: s = "aba"
Output: 2
Explanation: ['a', 'ba']
Example 3:

Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.

Solution

Problems like these are solved through recursions, where you want to extract a portion of string and repeat the same algorithm on the rest of string.

Lets look at the algorithm:

  • For checking unique substrings, you need to take a HashSet
  • We would take a substring, check it if its unique.
  • If its unique, we will move forward with rest of string in the same algorithm.

Lets look at one of example:

input=aba

a + (ba)
a + b + (a)
# a is duplicate, it will only (a, b)

Next iteration
ab + (a)

Next Iteration
aba + ()

Code

Lets look at the code:

private int find(String s, Set<String> set) {
  int max = 0;
  for (int i=0; i<s.length(); i++) {
    String sub = s.substring(0, i+1);
    if (!set.contains(sub)) {
      set.add(sub);
      max = Math.max(max, 1 + find(s.substring(i+1), set));
      
      set.remove(sub);
    }
  }
  
  return max;
}

public int maxUniqueSplit(String s) {
  Set<String> set = new HashSet<>();
  return this.find(s, set);
}

Complexity

Its O(n!), factorial of n.

Related Posts

Convert Roman to Integer number - Leet Code Solution

Convert Roman to Integer number - Leet Code Solution

Problem Statement Roman numerals are represented by seven different symbols: I…

Add two numbers(reverse order) link list Problem - Leet Code Solution

Add two numbers(reverse order) link list Problem - Leet Code Solution

Problem Statement You are given two non-empty linked lists representing two non…

Binary Search Tree (BST) Data Structure

Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes…

Binary Tree Data Structure

Binary Tree Data Structure

A Binary tree is a data structure which has two children nodes attached to it…

Find if Array contains Duplicate Number - Leet Code Solution

Find if Array contains Duplicate Number - Leet Code Solution

Problem Statement Given an array of integers, find if the array contains any…

Swap Nodes Pairs in Link List - Leet Code Solution

Swap Nodes Pairs in Link List - Leet Code Solution

Problem Statement Given a linked list, swap every two adjacent nodes and return…

Latest Posts

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…

Deep Dive on Caching: From Browser to Database

Deep Dive on Caching: From Browser to Database

“There are only two hard things in Computer Science: cache invalidation and…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…