Similar Problems

Similar Problems not available

Maximize Number Of Subsequences In A String - Leetcode Solution

Companies:

LeetCode:  Maximize Number Of Subsequences In A String Leetcode Solution

Difficulty: Medium

Topics: greedy string prefix-sum  

The problem description is as follows:

Given a string s, return the maximum number of unique subsequences of s such that each subsequence appears at least twice.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Example 1:

Input: "abcde" Output: 0 Explanation: There are no subsequences that appear at least twice.

Example 2:

Input: "abbaba" Output: 5 Explanation: The 5 subsequences are "aa", "bb", "ab", "ba", and "aba".

Solution:

To solve this problem, we need to find all the unique subsequences of the given string. Once we have all the subsequences, we can count how many of them appear at least twice.

To find all the unique subsequences, we can use the following approach:

  1. Generate all possible subsequences of the string s.
  2. Remove the duplicates from the list of subsequences.
  3. Return the list of unique subsequences.

To generate all the possible subsequences, we can use a recursive approach. The base case is when the input string is empty. In this case, we return an empty list. For the recursive case, we have two options: we can either include the first character of the string in the subsequence or we can exclude it. If we include the first character, we recursively call the function with the rest of the string and append the first character to each of the subsequences returned by the recursive call. If we exclude the first character, we recursively call the function with the rest of the string.

Once we have the list of all possible subsequences, we can remove the duplicates using a set. Finally, we can count how many of the unique subsequences appear at least twice by iterating through the list of unique subsequences and checking if each subsequence appears at least twice in the original string.

Here's the Python code that implements the above approach:

def allSubsequences(s): if not s: return [""] res = [] for seq in allSubsequences(s[1:]): res.append(seq) res.append(s[0] + seq) return res

def maxNumSubsequences(s): subseqs = set(allSubsequences(s)) count = 0 for subseq in subseqs: if s.count(subseq) >= 2: count += 1 return count

Test cases:

assert maxNumSubsequences("abcde") == 0 assert maxNumSubsequences("abbaba") == 5

Time Complexity:

The time complexity of the allSubsequences function is O(2^n) where n is the length of the input string. The time complexity of the set operation is O(nlogn). The time complexity of the loop that counts how many of the unique subsequences appear at least twice is O(kn) where k is the number of unique subsequences. Therefore, the overall time complexity of the solution is O(2^n + knlogn).

Space Complexity:

The space complexity of the allSubsequences function is O(2^n) where n is the length of the input string. The space complexity of the set operation is also O(2^n). Therefore, the overall space complexity of the solution is O(2^n).

Maximize Number Of Subsequences In A String Solution Code

1