Sort the letters in each word in lexicographical order. If the resulting sequences match, then the original words consist of the same letters and are therefore anagrams.
Algorithm implementation
Declare the strings and .
string s, q;Read the input strings.
cin >> s;
cin >> q;Sort the letters in each string.
sort(s.begin(),s.end());
sort(q.begin(),q.end());Compare the resulting strings and print the answer.
if (s == q) puts("YES"); else puts("NO");Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
char[] s = con.nextLine().toCharArray();
char[] q = con.nextLine().toCharArray();
Arrays.sort(s);
Arrays.sort(q);
if (Arrays.equals(s, q))
System.out.println("YES");
else
System.out.println("NO");
con.close();
}
}Python implementation
Read the input strings.
l1 = list(input())
l2 = list(input())Sort the letters in each string.
l1.sort()
l2.sort()Compare the resulting strings and print the answer.
if l1 == l2:
print('YES')
else:
print('NO')