Removed extra loop in Anagram.java (#3654)

This commit is contained in:
Taranjeet Singh Kalsi 2022-10-25 17:49:03 +05:30 committed by GitHub
parent 315e947c87
commit fd3386a0db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -121,13 +121,8 @@ public class Anagrams {
for (char c : t.toCharArray()) {
kk.put(c, kk.getOrDefault(c, 0) + 1);
}
// It checks for equal frequencies
for (char c : nm.keySet()) {
if (!nm.get(c).equals(kk.get(c))) {
return false;
}
}
return true;
// It checks for equal frequencies by comparing key-value pairs of two hashmaps
return nm.equals(kk);
}
}
}