-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
55 lines (48 loc) · 1.32 KB
/
Anagram.java
File metadata and controls
55 lines (48 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//{ Driver Code Starts
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String s1 = br.readLine(); // first string
String s2 = br.readLine(); // second string
Solution obj = new Solution();
if (obj.areAnagrams(s1, s2)) {
System.out.println("true");
} else {
System.out.println("false");
}
System.out.println("~");
}
}
}
// } Driver Code Ends
class Solution {
// Function is to check whether two strings are anagram of each other or not.
public static boolean areAnagrams(String s1, String s2) {
int[] arr=new int[26];
int idx;
for(int i=0;i<s1.length();i++)
{
idx=s1.charAt(i)-'a';
arr[idx]+=1;
}
for(int i=0;i<s2.length();i++)
{
idx=s2.charAt(i)-'a';
arr[idx]-=1;
}
for(int i=0;i<26;i++)
{
if(arr[i]!=0)
{
return false;
}
}
return true;
}
}