HackerRank Solution: Java Anagrams

Sakshi Singh
1 min readApr 15, 2019

--

import java.util.Scanner;

public class Solution {

static boolean isAnagram(String a, String b) {

String aa = a.toLowerCase();

String bb = b.toLowerCase();

char[] ch1 = aa.toCharArray();

char[] ch2 = bb.toCharArray();

int count[] = new int[26];

int n = ch1.length;

int m = ch2.length;

if(n != m)

return false;

for(int i=0;i<n;i++)

{

count[ch1[i]-97]++;

count[ch2[i]-97] — ;

}

for(int i=0;i<26;i++)

{

if (count[i] != 0)

return false;

}

return true;

}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String a = scan.next();

String b = scan.next();

scan.close();

boolean ret = isAnagram(a, b);

System.out.println( (ret) ? “Anagrams” : “Not Anagrams” );

}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sakshi Singh
Sakshi Singh

No responses yet

Write a response