#K62917. Minimum Steps to Make Two Strings Anagrams
Minimum Steps to Make Two Strings Anagrams
Minimum Steps to Make Two Strings Anagrams
Given two strings, determine the minimum number of character changes required to transform the first string into an anagram of the second string. In one step, you can change any character in the first string into any other character. If the strings have different lengths, it is impossible to convert one into an anagram of the other; in this case, output -1.
Let \( S_1 \) and \( S_2 \) be the two strings. If \(|S_1| \neq |S_2|\), output -1. Otherwise, the minimum number of changes required is given by:
[ \text{steps} = \sum_{c \in \Sigma} \max(0, \text{count}{S_1}(c) - \text{count}{S_2}(c)) ]
where \(\Sigma\) is the set of all characters, and \(\text{count}_{S}(c)\) denotes the frequency of character \(c\) in string \(S\).
inputFormat
The input consists of two lines. The first line contains the first string \(S_1\), and the second line contains the second string \(S_2\).
outputFormat
Output a single integer representing the minimum number of steps required to change \(S_1\) into an anagram of \(S_2\). If it is impossible (i.e., the strings have different lengths), output -1.
## samplelisten
silent
0