Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Jayvijay Shah
Why even hashmap is required. Calculate the ASCII sum of both strings and if both sums are equal it is an anagram

Jayvijay Shah

Dec 23, 2024

public boolean isAnagram(String s, String t) { int sum1 = 0; int sum2 = 0; for(int i = 0; i < s.length(); i++) { sum1 += (int) s.charAt(i); } for(int i = 0; i < t.length(); i++) { sum2 += (int) t.charAt(i); } return sum1 == sum2; }

0

0

Comments
Comments
Design Gurus
Design Gurus10 months ago

This approach will fail on this input:

s ="ggii"

t ="eekk"

This is because we can find two strings whose sum is the same but have different characters.