Skip to main content

Command Palette

Search for a command to run...

Remove Letter To Equalize Frequency

Published
2 min read
Remove Letter To Equalize Frequency
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.

Return true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise.

LeetCode Problem - 2423

class Solution {
    public boolean equalFrequency(String word) {
        // Create a map to store the frequency of each character in the input string
        Map<Character, Integer> frequencyMap = new HashMap<>();

        // Loop through each character in the word and update its frequency in the map
        for (char ch : word.toCharArray()) {
            frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
        }

        // Iterate through the characters in the frequency map
        for (char ch : frequencyMap.keySet()) {
            // Decrease the frequency of the current character by 1 to simulate its removal
            frequencyMap.put(ch, frequencyMap.get(ch) - 1);

            // Create a new map to count the frequencies of the remaining frequencies
            Map<Integer, Integer> freqCount = new HashMap<>();

            // Iterate through the values of the frequency map
            for (int freq : frequencyMap.values()) {
                // Only consider non-zero frequencies and update the frequency count map
                if (freq > 0) {
                    freqCount.put(freq, freqCount.getOrDefault(freq, 0) + 1);
                }
            }

            // If the frequency count map has only one unique frequency, return true
            if (freqCount.size() == 1) {
                return true;
            }

            // Restore the frequency of the current character after the check
            frequencyMap.put(ch, frequencyMap.get(ch) + 1);
        }

        // If no valid frequency condition is found, return false
        return false;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.