Skip to content

Commit fe91b19

Browse files
authored
Merge pull request codec-akash#51 from W-Larsen/ValidAnagram
Valid anagram
2 parents 526b2d1 + 92b8687 commit fe91b19

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

ValidAnagram.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given two strings s and t , write a function to determine if t is an anagram of s.
3+
* <p>
4+
* Example 1:
5+
* <p>
6+
* Input: s = "anagram", t = "nagaram"
7+
* Output: true
8+
* Example 2:
9+
* <p>
10+
* Input: s = "rat", t = "car"
11+
* Output: false
12+
*/
13+
public class ValidAnagram {
14+
15+
public boolean isAnagram(String s, String t) {
16+
if (s.length() > t.length()) {
17+
return false;
18+
}
19+
char[] secondWord = t.toCharArray();
20+
for (char letter : secondWord) {
21+
if (!isContainsLetter(s, letter)) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
private boolean isContainsLetter(String word, char specificLetter) {
29+
return word.contains(String.valueOf(specificLetter));
30+
}
31+
}

0 commit comments

Comments
 (0)