From d69a36cbf7d26934b3e1859a5b2804907e2998ee Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 17 May 2023 04:20:10 -0400 Subject: [PATCH] Complete deliverable --- problem-one.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ problem-two.js | 5 ++++ 2 files changed, 68 insertions(+) create mode 100644 problem-one.js create mode 100644 problem-two.js 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