Skip to main content

Command Palette

Search for a command to run...

Minimum Number of Steps to Make Two Strings Anagram

Published
1 min read
Minimum Number of Steps to Make Two Strings Anagram
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 two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.

Return the minimum number of steps to make t an anagram of s.

An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

LeetCode Problem - 1347

class Solution {
    public int minSteps(String s, String t) {

        // Create a map to store the frequency of each character in string 's'
        Map<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        int answer = 0;

        // Iterate through each character in string 't'
        for (char c : t.toCharArray()) {
            try {
                // Get the frequency of the current character from the map
                int value = map.get(c);
                // Decrease the frequency of the current character in the map
                map.put(c, map.getOrDefault(c, 0) - 1);
                // If the frequency becomes negative, increment the answer
                if (map.get(c) < 0) {
                    answer++;
                }
            } catch (Exception e) {
                // If the character is not found in the map, increment the answer
                answer++;
            }
        }

        // Return the total number of steps required
        return answer;
    }
}

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.