-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAnagram.java
28 lines (27 loc) · 923 Bytes
/
StringAnagram.java
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
//Given two strings s and t, return true if t is an anagram of s, and false otherwise.
//An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all
// the original letters exactly once.
import java.util.Arrays;
public class StringAnagram {
public static String sortS (String str) {
char c [] = str.toCharArray();
Arrays.sort(c);
return new String(c);
}
public static boolean checkAnagram (String s , String t) {
s = sortS(s);
t= sortS(t);
if (s.length()!= t.length()) {
return false;
}
for (int i=0 ; i< s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) return false;
}
return true;
}
public static void main(String[] args) {
String s = "ABCDE";
String t = "EDCBA";
System.out.println(checkAnagram(s, t));
}
}