fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class StringOperations
  9. {
  10. boolean anagram(String s1,String s2)
  11. {
  12. boolean ans=false;
  13. if(s1.length()==s2.length())
  14. {
  15. HashMap<Character,Integer> hp1=new HashMap<Character,Integer>();
  16. HashMap<Character,Integer> hp2=new HashMap<Character,Integer>();
  17. char[] arr1=s1.toCharArray();
  18. char[] arr2=s2.toCharArray();
  19. for(char a:arr1)
  20. {
  21. if(!hp1.containsKey(a))
  22. {
  23. hp1.put(a,1);
  24. }
  25. else
  26. {
  27. hp1.put(a,(hp1.get(a)+1));
  28. }
  29. }
  30. //heap 2
  31. for(char a:arr2)
  32. {
  33. if(!hp2.containsKey(a))
  34. {
  35. hp2.put(a,1);
  36. }
  37. else
  38. {
  39. hp2.put(a,(hp2.get(a)+1));
  40. }
  41. }
  42. int size=hp1.size();
  43. int count=0;
  44. //check if equal occurence
  45. for(Map.Entry<Character,Integer> map1:hp1.entrySet())
  46. {
  47. for(Map.Entry<Character,Integer> map2:hp2.entrySet())
  48. {
  49. if(map2.getKey()==map1.getKey() && map2.getValue()==map1.getValue())
  50. {
  51. count++;
  52. }
  53. }
  54. }
  55. if(count==size)
  56. return true;
  57. else
  58. return false;
  59. }
  60. return ans;
  61.  
  62. }
  63. public static void main (String[] args) throws java.lang.Exception
  64. {
  65. // your code goes here
  66. Scanner sc=new Scanner(System.in);
  67. String str1=sc.next();
  68. String str2=sc.next();
  69. StringOperations sb=new StringOperations();
  70.  
  71. if(sb.anagram(str1, str2))
  72. {
  73. System.out.println("Yes");
  74. }
  75. else
  76. {
  77. System.out.println("No");
  78. }
  79.  
  80.  
  81. }
  82. }
Success #stdin #stdout 0.05s 712704KB
stdin
abcd abab
stdout
No