leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/

 

Minimum Number of Steps to Make Two Strings Anagram - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Given two equal-size strings 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.

Input: s = "leetcode", t = "practice"
Output: 5
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
class Solution {
	public int minSteps(String s, String t) {
		int[] arr1 = new int[26];
		int[] arr2 = new int[26];

		for (int i = 0; i < s.length(); i++) {
			int temp = (int) s.charAt(i) - 'a';
			int temp2 = (int) t.charAt(i) - 'a';

			arr1[temp]++;
			arr2[temp2]++;
		}

		int answer = s.length();

		for (int i = 0; i < 26; i++) {
			answer -= Math.min(arr1[i], arr2[i]);
		}

		return answer;
	}
}

+ Recent posts