Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions problem-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// did this problem on leetcode: https://leetcode.com/problems/valid-anagram/

// initial solution:

const isAnagram = function (s, t) {
const sDictionary = {}
const tDictionary = {}
for (const letter of s) {
if (!sDictionary[letter]) {
sDictionary[letter] = 1
} else {
sDictionary[letter]++
}
}
for (const letter of t) {
if (!tDictionary[letter]) {
tDictionary[letter] = 1
} else {
tDictionary[letter]++
}
}

for (const letter of s) {
if (sDictionary[letter] !== tDictionary[letter]) {
return false
}
}

for (const letter of t) {
if (tDictionary[letter] !== sDictionary[letter]) {
return false
}
}
return true
}

// second attempt after looking at a hint:

const isAnagramB = function (s, t) {
const dictionary = {}
for (const letter of s) {
if (!dictionary[letter]) {
dictionary[letter] = 1
} else {
dictionary[letter]++
}
}
for (const letter of t) {
if (!dictionary[letter]) {
return false
} else {
dictionary[letter]--
}
}

for (const key in dictionary) {
if (dictionary[key] !== 0) {
return false
}
}

return true
}
5 changes: 5 additions & 0 deletions problem-two.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.