fork download
  1. import java.util.*;
  2. import java.io.*;
  3. public class Main
  4. {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. String a =sc.nextLine();
  9. String b =sc.nextLine();
  10. int n1 =a.length();
  11. int n2 =b.length();
  12. int n=n1;
  13.  
  14. boolean res0=true;
  15. if(n1!=n2){
  16. res0=false;
  17. }
  18.  
  19. HashMap<Character,Integer>m1 = new HashMap<>();
  20. HashMap<Character,Integer>m2 = new HashMap<>();
  21. HashMap<Character,Integer>m3 = new HashMap<>();
  22. HashMap<Character,Integer>m4 = new HashMap<>();
  23.  
  24. for(int i=0;i<n;i++){
  25. if(i%2 ==0){ //even indices
  26. m1.put(a.charAt(i),m1.getOrDefault(a.charAt(i),0)+1);
  27. m3.put(b.charAt(i),m3.getOrDefault(b.charAt(i),0)+1);
  28. }
  29. else if((i%2)!=0){
  30. m2.put(a.charAt(i),m2.getOrDefault(a.charAt(i),0)+1);
  31. m4.put(b.charAt(i),m4.getOrDefault(b.charAt(i),0)+1);
  32. }
  33. }
  34.  
  35. boolean f1= false;
  36. boolean f2= false;
  37. boolean res1 =false;
  38. int flag1=0;
  39. // if(m1.equals(m3)){
  40. // res1=true;
  41. // }
  42. if(m1.size() != m3.size()){
  43. res1=false;
  44. }
  45. else if(m1.isEmpty() && m3.isEmpty()){
  46. res1=true;
  47. }
  48. else{
  49. for(Map.Entry<Character,Integer>i :m1.entrySet()){
  50. Character k =i.getKey();
  51. int v =i.getValue();
  52. f1=false;
  53. f2=false;
  54.  
  55. if(m3.containsKey(k)){
  56. f1=true;
  57. int val =m3.get(k);
  58.  
  59. if(val == v){
  60. f2= true;
  61. }
  62. }
  63. if(f1==false || f2 ==false){
  64. res1=false;
  65. flag1=1;
  66. break;
  67. }
  68. }
  69. if(flag1==0){
  70. res1=true;
  71. }
  72. }
  73.  
  74.  
  75.  
  76. boolean f3= false;
  77. boolean f4= false;
  78. boolean res2 =false;
  79. int flag2=0;
  80. // if(m2.equals(m4)){
  81. // res2=true;
  82. // }
  83. if(m2.size() != m4.size()){
  84. res2=false;
  85. }
  86. else if(m2.isEmpty() && m4.isEmpty()){
  87. res2=true;
  88. }
  89. else{
  90. for(Map.Entry<Character,Integer>i :m2.entrySet()){
  91. Character k =i.getKey();
  92. int v =i.getValue();
  93. f3=false;
  94. f4=false;
  95. if(m4.containsKey(k)){
  96. f3=true;
  97. int val =m4.get(k);
  98.  
  99. if(val == v){
  100. f4= true;
  101. }
  102. }
  103. if(f3==false || f4==false){
  104. res2=false;
  105. flag2=1;
  106. break;
  107. }
  108. }
  109. if(flag2 == 0){
  110. res2=true;
  111. }
  112.  
  113. }
  114.  
  115.  
  116. if(res1 ==true && res2 ==true && res0 == true){
  117. System.out.print("Strings are equal when operation done");
  118. }
  119. else{
  120. System.out.print("Strings are not equal even when operation done");
  121. }
  122.  
  123.  
  124. }
  125. }
Runtime error #stdin #stdout #stderr 0.13s 56648KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
	at Main.main(Main.java:8)