-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsAnagram.js
More file actions
61 lines (53 loc) · 1.32 KB
/
IsAnagram.js
File metadata and controls
61 lines (53 loc) · 1.32 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
*
* Is Anagram
* Solved
* Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
* An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
* Example 1:
* Input: s = "racecar", t = "carrace"
* Output: true
* Example 2:
* Input: s = "jar", t = "jam"
* Output: false
* Constraints:
* s and t consist of lowercase English letters.
*/
class Solution {
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
isAnagram(s, t) {
if (s.length !== t.length) {
return false;
}
const charCount = {};
// Count characters in the first string
for (let char of s) {
charCount[char] = (charCount[char] || 0) + 1;
}
// Subtract counts using the second string
for (let char of t) {
if (!charCount[char]) {
return false;
}
charCount[char]--;
if (charCount[char] < 0) {
return false;
}
}
return true;
}
}
// Example Usage:
const solution = new Solution();
// Example 1
const s1 = "racecar";
const t1 = "carrace";
console.log(solution.isAnagram(s1, t1)); // Output: true
// Example 2
const s2 = "jar";
const t2 = "jam";
console.log(solution.isAnagram(s2, t2)); // Output: false