Fewest Replacements Making One String an Anagram

Quick Overview

Fewest Replacements Making One String an Anagram. The lowercase strings s and t have equal length. One operation replaces any character of t with another character.

Fewest Replacements Making One String an Anagram

Company: JPMorgan Chase

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The lowercase strings s and t have equal length. One operation replaces any character of t with another character. Return the minimum operations making t have exactly the same character multiset as s; order need not match. ```cpp class Solution { public: int minSteps(string s, string t); }; ``` ### Examples **Example 1:** ```text Input: s = "aabc", t = "dddd" Output: 4 Explanation: All four d characters must become the two a letters,b,and c. ``` **Example 2:** ```text Input: s = "abca", t = "cbaa" Output: 0 Explanation: Their letter counts already agree. ``` ### Constraints - Both strings share a length between 1 and 50000. - Only lowercase English letters occur. ### Contract considerations Anagram means equal multisets, not necessarily equal position-by-position strings. ```hint Read the boundary condition Account for repeated letters and strings that are already anagrams. Only the second string may be changed. ``` ### Boundary reminders Anagram means equal multisets, not necessarily equal position-by-position strings.

Overview: Fewest Replacements Making One String an Anagram. The lowercase strings s and t have equal length. One operation replaces any character of t with another character.

JPMorgan Chase logo
JPMorgan Chase
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

The lowercase strings s and t have equal length. One operation replaces any character of t with another character. Return the minimum operations making t have exactly the same character multiset as s; order need not match.

class Solution {
public:
    int minSteps(string s, string t);
};

Examples

Example 1:

Input: s = "aabc", t = "dddd"
Output: 4
Explanation: All four d characters must become the two a letters,b,and c.

Example 2:

Input: s = "abca", t = "cbaa"
Output: 0
Explanation: Their letter counts already agree.

Constraints

  • Both strings share a length between 1 and 50000.
  • Only lowercase English letters occur.

Contract considerations

Anagram means equal multisets, not necessarily equal position-by-position strings.

Boundary reminders

Anagram means equal multisets, not necessarily equal position-by-position strings.