import
java.util.*;
public
class
Main {
static
void
insertAndRemove(Map<Character, Integer> mp,
char
toInsert,
char
toRemove)
{
mp.put(toInsert, mp.getOrDefault(toInsert,
0
) +
1
);
mp.put(toRemove, mp.getOrDefault(toRemove,
0
) -
1
);
if
(mp.get(toRemove) ==
0
)
mp.remove(toRemove);
}
static
boolean
balanceOccurrences(String word1,
String word2)
{
Map<Character, Integer> mp1 =
new
HashMap<>();
Map<Character, Integer> mp2 =
new
HashMap<>();
for
(
char
w1 : word1.toCharArray())
mp1.put(w1, mp1.getOrDefault(w1,
0
) +
1
);
for
(
char
w2 : word2.toCharArray())
mp2.put(w2, mp2.getOrDefault(w2,
0
) +
1
);
for
(
char
c1 =
'a'
; c1 <=
'z'
; c1++) {
for
(
char
c2 =
'a'
; c2 <=
'z'
; c2++) {
if
(!mp1.containsKey(c1)
|| !mp2.containsKey(c2))
continue
;
insertAndRemove(mp1, c2, c1);
insertAndRemove(mp2, c1, c2);
if
(mp1.size() == mp2.size())
return
true
;
insertAndRemove(mp1, c1, c2);
insertAndRemove(mp2, c2, c1);
}
}
return
false
;
}
public
static
void
main(String[] args)
{
String word1 =
"cdefg"
;
String word2 =
"hijkl"
;
if
(balanceOccurrences(word1, word2))
System.out.println(
"True"
);
else
System.out.println(
"False"
);
}
}