Problem · String
Get Minimum Difference
Learn this problemProblem statement
An anagram is a word whose characters can be rearranged to create another word. Given two strings, determine the minimum number of characters in either string that must be modified to make the two strings anagrams. If it is not possible to make the two strings anagrams, return -1.
Function
getMinimumDifference(a: String[], b: String[]) → int[]
Complete the function getMinimumDifference in the editor.
getMinimumDifference has the following parameter(s):
String[] a: an array of stringsString[] b: an array of strings
Returns
int[]: the minimum number of characters in either string that needs to be modified to make the two strings anagrams or -1 if it is not possible
Examples
Example 1
a = ["tea", "tea", "act"]b = ["ate", "toe", "acts"]return = [0, 1, -1]Perform the following calculations:
- a[0] = tea and b[0] = ate are anagrams, so 0 characters need to be modified.
- a[1] = tea and b[1] = toe are not anagrams. Modify 1 character in either string (o → a or a → o) to make them anagrams.
- a[2] = act and b[2] = acts are not anagrams and cannot be converted to anagrams because they contain different numbers of characters.
Example 2
a = ["a", "jk", "abb", "mn", "abc"]b = ["bb", "kj", "bbc", "op", "def"]return = [-1, 0, 1, 2, 3]Perform the following n = 5 calculations:
- Index 0: a and bb cannot be anagrams because they contain different numbers of characters.
- Index 1: jk and kj are already anagrams because they both contain the same characters at the same frequencies.
- Index 2: abb and bbc differ by one character.
- Index 3: mn and op differ by two characters.
- Index 4: abc and def differ by three characters.
Constraints
Each string consists of lowercase characters [a-z]1 ≤ n ≤ 1000 ≤ |a[i]|, |b[i]| ≤ 1041 ≤ |a[i]| + |b[i]| ≤ 104More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026