forked from codec-akash/DSA-PS-Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
31 lines (29 loc) · 759 Bytes
/
ValidAnagram.java
File metadata and controls
31 lines (29 loc) · 759 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
29
30
31
/**
* Given two strings s and t , write a function to determine if t is an anagram of s.
* <p>
* Example 1:
* <p>
* Input: s = "anagram", t = "nagaram"
* Output: true
* Example 2:
* <p>
* Input: s = "rat", t = "car"
* Output: false
*/
public class ValidAnagram {
public boolean isAnagram(String s, String t) {
if (s.length() > t.length()) {
return false;
}
char[] secondWord = t.toCharArray();
for (char letter : secondWord) {
if (!isContainsLetter(s, letter)) {
return false;
}
}
return true;
}
private boolean isContainsLetter(String word, char specificLetter) {
return word.contains(String.valueOf(specificLetter));
}
}