You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sid loves to read short stories. Being a Computer Science student, he decides to do some frequency analysis on his favorite reading material. For each data point, chooses a string of length a from one book, and a string of length b from a second book. The strings' lengths differ by no more than 1.
14
+
|a-b|≤1, where |x| represents the absolute value function.
15
+
16
+
The frequency analysis consists of checking how far the strings are from being anagrams of one another. Your challenge is to help him find the minimum number of characters of the first string he needs to change to make it an anagram of the second string. He can neither add nor delete characters from the first string. Only replacement of the characters with new ones is allowed.
17
+
18
+
Input Format
19
+
The first line will contain an integer T representing the number of test cases. Each test case will contain a string having length (a+b) which will be concatenation of both the strings described in problem. The string will only contain small letters and without any spaces.
20
+
21
+
Output Format
22
+
An integer corresponding to each test case is printed in a different line i.e., the number of changes required for each test case. Print ‘-1’ if it is not possible.
23
+
24
+
Constraints
25
+
1 ≤ T ≤ 100
26
+
1 ≤ a+b ≤ 10,000
27
+
28
+
Sample Input
29
+
5
30
+
aaabbb
31
+
ab
32
+
abc
33
+
mnop
34
+
xyyx
35
+
Sample Output
36
+
3
37
+
1
38
+
-1
39
+
2
40
+
0
41
+
42
+
Explanation
43
+
In the five test cases
44
+
One string must be “aaa” and the other “bbb”. The lengths are a=3 and b=3, so the difference is less than 1. No characters are common between the strings, so all three must be changed.
45
+
One string must be “a” and the second “b”. The lengths are a=1 and b=1, so the difference is less than 1. One character must be changed to them the same.
46
+
Since the string lengths a and b must differ by no more than 1, the lengths are either a=1 and b=2 or a=2 and b=1. No sequence of substitutions will make the two anagrams of one another.
47
+
One string must be “mn" and other be “op”. The length are a=2 and b=2, so the difference is less than 1. No characters are common between the strings, so both must be changed.
48
+
One string must be “xy” and the other be “yx”. The length are a=2 and b=2, so the difference is less than 1. No changes are needed because the second string is already an anagram of the first.
49
+
*/
50
+
51
+
52
+
/*
53
+
Check if they are valid string. If length == odd, then fail, -1
54
+
Use int[26] arr to add chars from s1. If non-26-char exist, return -1
55
+
use same int arr to remove chars from s2.
56
+
count the non-zeros
57
+
58
+
Note: the order of letters does not matter, becase all we want to change to is anagram
0 commit comments