Similar Problems

Similar Problems not available

Subsequence With The Minimum Score - Leetcode Solution

Companies:

LeetCode:  Subsequence With The Minimum Score Leetcode Solution

Difficulty: Hard

Topics: string binary-search two-pointers  

Problem Statement:

Given a string s containing only lowercase letters, you need to find the size of the smallest non-empty subsequence of s such that the frequency of each character in it is greater than or equal to 2.

Approach: To solve the problem, we can use a greedy approach. We first count the frequency of each character in the input string. We then create a set of characters that appear in the input string at least twice. We then create a list of tuples (character, frequency) of the remaining characters in the input string (i.e. those that appear only once). We then sort this list in descending order of frequency. We then iterate through the sorted list and add the current character to the output string if it does not appear in the set created earlier. We also add the current character to the set. We stop iterating when the set contains all characters in the input string that appear at least twice.

Example: Let's consider the input string "cbacdcbc". First, we count the frequency of each character in the input string. Here, we get: c: 3 b: 2 a: 1 d: 1

Next, we create a set of characters that appear at least twice in the input string. Here, we get: {'c', 'b'}

Next, we create a list of tuples (character, frequency) for those characters that appear only once in the input string. Here, we get: [('a', 1), ('d', 1)]

Next, we sort the list in descending order of frequency. Here, we get: [('a', 1), ('d', 1)]

We then iterate through the sorted list. We first encounter the character 'a'. Since 'a' does not appear in the set created earlier, we add it to the output string and add it to the set. Here, we get: {'a', 'c', 'b'}

Next, we encounter the character 'd'. Since 'd' does not appear in the set created earlier, we add it to the output string and add it to the set. Here, we get: {'a', 'c', 'b', 'd'}

We stop iterating since the set contains all characters that appear at least twice in the input string. The output string is "adcbd". The size of this subsequence is 5.

Solution:

class Solution: def smallestSubsequence(self, s: str) -> str: # count the frequency of each character in the input string freq = {} for c in s: freq[c] = freq.get(c, 0) + 1

    # create a set of characters that appear at least twice in the input string
    chars = set([c for c in freq if freq[c] >= 2])

    # create a list of tuples (character, frequency) for those characters that appear only once in the input string
    remaining = [(c, freq[c]) for c in freq if freq[c] == 1]

    # sort the list in descending order of frequency
    remaining.sort(key=lambda x: x[1], reverse=True)

    # iterate through the sorted list and add the current character to the output string if it does not appear in the set created earlier
    output = ''
    for c, f in remaining:
        if c not in chars:
            output += c
            chars.add(c)
        # stop iterating when the set contains all characters in the input string that appear at least twice
        if chars == set(freq.keys()):
            break

    return output

Time Complexity: O(nlogn) where n is the length of the input string. This is because we need to sort the list of remaining characters.

Space Complexity: O(n) where n is the length of the input string. This is because we need to store the frequency of each character in the input string, the list of remaining characters, and the set of characters that appear at least twice in the input string.

Subsequence With The Minimum Score Solution Code

1