-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidAnagram2.js
More file actions
30 lines (22 loc) · 913 Bytes
/
Copy pathvalidAnagram2.js
File metadata and controls
30 lines (22 loc) · 913 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
function isValidAnagram(firstString,secondString){
console.log(firstString.length)
console.log(secondString.length)
if(firstString.length!==secondString.length) return false
const firstStringHashMap={}
// console.log("Oh HI")
const secondStringHashMap={}
for (let i=0;i<firstString.length;i++){
firstStringHashMap[firstString[i]]=(firstStringHashMap[firstString[i]]||0)+1
secondStringHashMap[secondString[i]]=(secondStringHashMap[secondString[i]]||0)+1
}
console.log("first has Map",firstStringHashMap)
console.log("second string hashmap",secondStringHashMap)
for (const key in firstStringHashMap){
if(firstStringHashMap[key]!==secondStringHashMap[key]) return false
}
return true
}
const s = "racecar"
const t = "carrace"
const result=isValidAnagram(s,t)
console.log(result)