Question:
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m=sc.nextInt();
for(int mi=0;mi<m;mi++){
String s1=sc.next();
String s2=sc.next();
if(s1.length()!=s2.length()){System.out.println("NO");}
else{
if(sort(s1).equals(sort(s2))){System.out.println("YES");}
else{System.out.println("NO");}
}
}
}
public static String sort(String s){
char[] ar=s.toCharArray();
Arrays.sort(ar);
return new String(ar);
}
}
Given two strings, check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “act” and “tac” are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of two strings in 'lowercase' only, in a separate line.
Output:
Print "YES" without quotes if the two strings are anagram else print "NO".
Constraints:
1 ≤ T ≤ 30
1 ≤ |s| ≤ 100
Example:
Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
Output:
YES
NO
YES
NO
CODE:
import java.util.*;import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m=sc.nextInt();
for(int mi=0;mi<m;mi++){
String s1=sc.next();
String s2=sc.next();
if(s1.length()!=s2.length()){System.out.println("NO");}
else{
if(sort(s1).equals(sort(s2))){System.out.println("YES");}
else{System.out.println("NO");}
}
}
}
public static String sort(String s){
char[] ar=s.toCharArray();
Arrays.sort(ar);
return new String(ar);
}
}
EXECUTION TIME:0.09s
Comments
Post a Comment