We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
we can not use any library to since import is already disbaled.
we can use the loop to find the frequency of every char and we can check if the values of count of each char is equal in every iteration and if length of string is equal then frequency will never gets equal.
importjava.util.Scanner;publicclassSolution{staticbooleanisAnagram(Stringa,Stringb){// Complete the functionif(a.length()!=b.length())returnfalse;a=a.toLowerCase();b=b.toLowerCase();for(charc:a.toCharArray()){intcounta=0;intcountb=0;for(intj=0;j<a.length();j++){if(c==a.charAt(j))counta++;if(c==b.charAt(j))countb++;}if(counta!=countb)returnfalse;}returntrue;}publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);Stringa=scan.next();Stringb=scan.next();scan.close();booleanret=isAnagram(a,b);System.out.println((ret)?"Anagrams":"Not Anagrams");}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
we can not use any library to since import is already disbaled. we can use the loop to find the frequency of every char and we can check if the values of count of each char is equal in every iteration and if length of string is equal then frequency will never gets equal.