Java Anagrams

  • + 0 comments
    1. for example, peh and hpe were the inputs. After converting it to uppercase we get PEH and HPE.
      • c[a.charAt(i) - 'A']++; This statement inside the loop fetches the character from the string
      • Pass1: c['P'-'A']++ will subtract the ASCII values of P and A i.e 80-65=15. It'll increase the value at c[15] by 1. Basically c represents all the alphabets. c[0] will hold the frequency of A, c[1] will hold the frequncy of B,... c[15] will hold the frequency of P, and so on. Then in the last loop we're just comparing all the values in array and if they're not equal thenbreak the loop by returning false