diff --git a/problem-one.js b/problem-one.js new file mode 100644 index 0000000..d9086da --- /dev/null +++ b/problem-one.js @@ -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 +} diff --git a/problem-two.js b/problem-two.js new file mode 100644 index 0000000..374cb5f --- /dev/null +++ b/problem-two.js @@ -0,0 +1,5 @@ +// tried this leetcode in python: https://leetcode.com/problems/reverse-linked-list/ + +// and got ******* STUCK for like 45 minutes, very frustrating because we just looked at linked lists today but something about they way they set you up to start (`class Solution`? wtf) really confused me. on the upside I just looked at the comments and there are a lot of other participants who also found the question setup confusing. + +// my approach on this would be to instantiate an empty list, then iterate over the passed in list and add each node in passed in list to the front of my new list \ No newline at end of file