Similar Problems

Similar Problems not available

Make Number Of Distinct Characters Equal - Leetcode Solution

Companies:

LeetCode:  Make Number Of Distinct Characters Equal Leetcode Solution

Difficulty: Medium

Topics: string hash-table  

Problem Summary:

The problem asks us to find the smallest substring of the given string, which contains all the distinct characters present in the string. This means that if the string has 5 distinct characters, then we need to find the smallest substring that has all these 5 distinct characters.

Solution Approach:

One way of approaching this problem is by using a sliding window technique. We can use two pointers, left and right, to represent the current substring and move them accordingly to find the smallest substring that has all the distinct characters.

The basic approach is to move the right pointer until we find all distinct characters and then move the left pointer to find the smallest substring that contains all distinct characters.

First, we need to count the frequency of each character in the string using a HashMap. Then we can move the right pointer until we find all the distinct characters. Once we find all the distinct characters, we can move the left pointer until we lose one of the distinct characters. We then move the right pointer again until we find all the distinct characters. We repeat this process until we reach the end of the string. While moving the pointers, we keep track of the minimum length of the substring that contains all distinct characters.

Here is the code for this approach:

public int makeStringDistinct(String s) {
    // create map to store frequency of characters
    Map<Character, Integer> map = new HashMap<>();
    
    // initialize pointers and minimum length
    int left = 0, right = 0, minLen = Integer.MAX_VALUE;
    
    // loop through the string
    while (right < s.length()) {
        char c = s.charAt(right);
        
        // update map with frequency of character
        map.put(c, map.getOrDefault(c, 0) + 1);
        
        // move right pointer until we find all distinct characters
        while (map.size() == (right - left + 1)) {
            // update min length
            minLen = Math.min(minLen, right - left + 1);
            
            // move left pointer to find smallest substring
            char leftChar = s.charAt(left);
            map.put(leftChar, map.get(leftChar) - 1);
            if (map.get(leftChar) == 0) {
                map.remove(leftChar);
            }
            left++;
        }
        
        right++;
    }
    
    return minLen == Integer.MAX_VALUE ? 0 : minLen;
}

Time Complexity:

The time complexity of this approach is O(n), where n is the length of the input string. This is because we are traversing the string only once and using a HashMap to store the frequency of characters, which takes O(1) time for each operation.

Space Complexity:

The space complexity of this approach is O(k), where k is the number of distinct characters in the string. This is because we are using a HashMap to store the frequency of characters, which can have a maximum of k keys.

Make Number Of Distinct Characters Equal Solution Code

1