forked from ElminD/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
32 lines (25 loc) · 1.03 KB
/
ValidAnagram.java
File metadata and controls
32 lines (25 loc) · 1.03 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
//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.
//Example 1:
// Input: s = "anagram", t = "nagaram"
// Output: true
//Example 2:
// Input: s = "rat", t = "car"
// Output: false
class Solution {
public boolean isAnagram(String s, String t) {
//check if the string are not the same length
if (s.length() != t.length()) return false;
//create a int the size of the alphabet
int[] store = new int[26];
//loop through the array adding the count of each letter and removing it if its in array t
for (int i = 0; i < s.length(); i++) {
store[s.charAt(i) - 'a']++;
store[t.charAt(i) - 'a']--;
}
//go though the array and check if there is a letter not counted for
for (int n : store) if (n != 0) return false;
return true;
}
}