We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1836a8f commit 7a7a020Copy full SHA for 7a7a020
valid-anagram/f-euxan21.java
@@ -1,19 +1,16 @@
1
// time : O(n)
2
-// space : O(n)
+// space : O(1)
3
4
class Solution {
5
public boolean isAnagram(String s, String t) {
6
- char[] sToChar = s.toCharArray();
7
- char[] tToChar = t.toCharArray();
+
+ if(s.length() != t.length()) return false;
8
9
int[] charCount = new int[26];
10
11
- for(char c : sToChar) {
12
- charCount[c - 'a']++;
13
- }
14
-
15
- for(char c : tToChar) {
16
- charCount[c - 'a']--;
+ for(int i = 0; i < s.length(); i++) {
+ charCount[s.charAt(i) - 'a']++;
+ charCount[t.charAt(i) - 'a']--;
17
}
18
19
for(int i : charCount) {
0 commit comments