You are viewing a single comment's thread. Return to all comments →
Using HashMap
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc=new Scanner(System.in); String s1=sc.nextLine().toLowerCase(); String s2=sc.nextLine().toLowerCase(); String[] str1=s1.split(""); String[] str2=s2.split(""); HashMap<String,Integer> hs1=new HashMap<>(); HashMap<String,Integer> hs2=new HashMap<>(); for(int i=0;i<str1.length;i++){ if(hs1.containsKey(str1[i])){ hs1.put(str1[i],hs1.get(str1[i])+1); } else{ hs1.put(str1[i],1); } } for(int i=0;i<str2.length;i++){ if(hs2.containsKey(str2[i])){ hs2.put(str2[i],hs2.get(str2[i])+1); } else{ hs2.put(str2[i],1); } } if(hs1.equals(hs2)){ System.out.println("Anagrams"); } else{ System.out.println("Not Anagrams"); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
Using HashMap
import java.io.; import java.util.;
public class Solution {
}