Intro
I call two \$n\$-strings \$s\$ and \$z\$ over the alphabet \$\Sigma\$ weakly isomorphic if their character frequency multisets are equal. In other words, for an input strings \$s\$, we derive a function \$f_s\colon\Sigma \rightarrow \mathbb{N}\$ such that for any character of \$c \in s\$, the value \$f_s(c)\$ is the number of times \$c\$ appeared in \$s\$. Finally we take the multiset of the range \$\mathcal{R}(f_s)\$ of \$f_s\$. We, then, derive the \$\mathcal{R}(f_z)\$ and return true if and only if \$\mathcal{R}(f_s) = \mathcal{R}(f_z)\$.
Code
public static boolean areWeaklyIsomorphic(String s, String z) {
Objects.requireNonNull(s, "The first input string is null");
Objects.requireNonNull(z, "The second input string is null");
if (s.length() != z.length()) {
return false;
}
Map<Character, Integer> counterMapS = new HashMap<>();
Map<Character, Integer> counterMapZ = new HashMap<>();
for (char ch : s.toCharArray()) {
counterMapS.put(ch, counterMapS.getOrDefault(ch, 0) + 1);
}
for (char ch : z.toCharArray()) {
counterMapZ.put(ch, counterMapZ.getOrDefault(ch, 0) + 1);
}
if (counterMapS.size() != counterMapZ.size()) {
return false;
}
List<Integer> counterListS = new ArrayList<>(counterMapS.values());
List<Integer> counterListZ = new ArrayList<>(counterMapZ.values());
counterListS.sort(Integer::compare);
counterListZ.sort(Integer::compare);
return counterListS.equals(counterListZ);
}