2716. Minimize String Length

EasyHash TableString
Leetcode Link

Problem Description

In this problem, we are given a 0-indexed string s. The operation we can perform on this string is to delete the closest occurrences of a character c to the left and right of index i, where c is the character at index i. We can perform this operation any number of times with the goal to minimize the length of string s as much as possible. The task is to return the length of the string after performing the operation optimally to minimize the string length.

Intuition

To solve this problem, we need to find a way to reduce the string length as much as possible by deleting characters. One might initially think that keeping track of the occurrences and their positions is required. However, on careful observation, we can note that in the best case scenario, each unique character can completely remove its closest occurrence to the left and right for every other duplicate character.

If we consider an example: Given a string s = "abcddcba", we can see this:

  • For 'a', the closest 'a' to the left of the second 'a' is the first 'a', and similarly for the closest to the right.
  • The same argument applies to 'b', 'c', and 'd'.

The optimal removal would remove all characters except for one instance of each unique character. Thus, our problem simplifies to counting the number of unique characters in the string since in the minimized string, every character that has duplicates would have only one instance left.

Therefore, the solution approach to finding the length of the minimized string is to simply return the count of unique characters in the string, which can be found using the set() function in Python that returns all unique elements, and then using the len() function to count these unique elements.

Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece:

Which of the following array represent a max heap?

Solution Approach

The implementation of the solution is straightforward because of the simplicity of the problem once we understand the pattern. We utilize the fact that for any character that has duplicates in the string, all its occurrences except one will eventually be removed. By following this logic, we can infer that the solution does not require complex algorithms or data structures.

The approach consists of two steps:

  1. Convert the string into a set of characters, which eliminates any duplicate occurrences of characters. We use the set() constructor in Python, which takes an iterable, such as a string, and returns a new set object with distinct elements.
  2. Calculate the length of this set, which gives us the number of unique characters in the original string. To get the count, we use the len() function which returns the number of items in a container.

By employing these built-in functions, set and len, we efficiently solve the problem without the need for any additional data structure or algorithm. The pattern recognized here is essentially a unique element count problem which often leads to the use of sets for their property of containing non-duplicate elements.

The Python code is as follows:

1class Solution:
2    def minimizedStringLength(self, s: str) -> int:
3        return len(set(s))

In the code, s is the input string. We pass s to the set() function which removes all duplicate characters. Then, we pass the resulting set to the len() function to get the total count of unique characters. This count represents the length of the minimized string, according to the problem statement.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

Which algorithm should you use to find a node that is close to the root of the tree?

Example Walkthrough

Let's illustrate the solution approach with a small example. Consider the string s = "abacabad". According to the problem, we want to delete occurrences of characters that are closest to the left and right of any instance of that character. Following the solution approach, here's a step-by-step walkthrough:

  1. Convert the string s into a set of characters to get the unique characters. For our example, converting s to a set would look like this:
1unique_chars = set("abacabad")
2print(unique_chars)  # Output will be {'a', 'b', 'c', 'd'}

After converting to a set, we have {'a', 'b', 'c', 'd'}, which represents the unique characters in the string.

  1. Calculate the length of this set to find out how many unique characters we have:
1length = len(unique_chars)
2print(length)  # Output will be 4

The length of the unique character set is 4, which means there are four unique characters in the string s.

Following the intuition of the problem, these four unique characters would be the characters that remain after optimally performing the given operation (i.e., deleting the closest occurrences of a character to the left and right of index i). Therefore, the length of the string after performing the operations is the same as the number of unique characters in the original string, which is 4 in this case.

Thus, using the set() function in Python, we eliminated all the duplicates, and by using the len() function, we efficiently found the minimized string length. This example perfectly illustrates how the solution approach leads to an optimal and simplistic answer.

Solution Implementation

1class Solution:
2    def minimizedStringLength(self, s: str) -> int:
3        # Create a set from the string 's' to eliminate any duplicate characters.
4        unique_characters = set(s)
5
6        # The minimized string length is equal to the number of unique characters.
7        minimized_length = len(unique_characters)
8
9        # Return the length of the minimized string.
10        return minimized_length
11
1class Solution {
2    // Function to compute the minimized string length based on unique characters
3    public int minimizedStringLength(String inputString) {
4        // Create a HashSet to store unique characters
5        Set<Character> uniqueCharacters = new HashSet<>();
6      
7        // Iterate through all characters in the input string
8        for (int i = 0; i < inputString.length(); ++i) {
9            // Add each character to the Set, duplicates are automatically ignored
10            uniqueCharacters.add(inputString.charAt(i));
11        }
12      
13        // The size of the Set represents the number of unique characters
14        // Return this size as the minimized string length
15        return uniqueCharacters.size();
16    }
17}
18
1#include <unordered_set> // Include necessary header for unordered_set
2#include <string> // Include necessary header for string
3
4class Solution {
5public:
6    // Function to calculate the minimized string length
7    int minimizedStringLength(std::string s) {
8        // Create an unordered_set to store unique characters
9        std::unordered_set<char> unique_characters(s.begin(), s.end());
10
11        // The size of the set gives us the count of unique characters
12        return unique_characters.size();
13    }
14};
15
1// This function calculates the minimized length of a string 
2// by removing all duplicate characters.
3// @param {string} inputString - The string to be processed.
4// @returns {number} The count of unique characters in the input string.
5
6function minimizedStringLength(inputString: string): number {
7    // Split the input string into its individual characters,
8    // convert it into a Set to remove duplicates,
9    // and then return the size of the Set.
10    return new Set(inputString.split('')).size;
11}
12
Not Sure What to Study? Take the 2-min Quiz:

Which of the following is equvalent to O(3*2^n + n^3 + n!+ log n)?

Time and Space Complexity

Time Complexity

The time complexity of the minimizedStringLength function is O(n), where n is the length of the string s. This is because the function iterates through each character in the string exactly once to create a set of unique characters.

Space Complexity

The space complexity of the function is O(k), where k is the number of unique characters in the string s. In the worst case, if all characters are unique, k would be equal to n, resulting in a space complexity of O(n). However, considering the input is a string with a fixed character set (e.g., ASCII characters), k can also be considered a constant, and hence the space complexity can be O(1) in the context of a fixed character set.

Learn more about how to find time and space complexity quickly using problem constraints.

Fast Track Your Learning with Our Quick Skills Quiz:

Given an array of 1,000,000 integers that is almost sorted, except for 2 pairs of integers. Which algorithm is fastest for sorting the array?


Recommended Readings


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫