-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathValidAnagram.java
More file actions
28 lines (23 loc) · 861 Bytes
/
ValidAnagram.java
File metadata and controls
28 lines (23 loc) · 861 Bytes
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
class Solution {
public boolean isAnagram(String s, String t) {
// if the lengths are not equal, then it cannot be an anagram
if(s.length() != t.length()){
return false;
}
// converting the string s to array and the sorting it and making it a string
char[] temp = s.toCharArray();
Arrays.sort(temp);
s = new String(temp);
// initializing temp with null to reuse it for string t
temp = null;
// converting the string t to array and the sorting it and making it a string
temp = t.toCharArray();
Arrays.sort(temp);
t = new String(temp);
// if both the strings are equal, return true else return false
if(s.equalsIgnoreCase(t)){
return true;
}
return false;
}
}