From 297341270572de6e94938e6845daeb6e256a8136 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 12:51:52 +0900 Subject: [PATCH 01/10] solution: valid anagram --- valid-anagram/flynn.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-anagram/flynn.py diff --git a/valid-anagram/flynn.py b/valid-anagram/flynn.py new file mode 100644 index 000000000..b45d142a5 --- /dev/null +++ b/valid-anagram/flynn.py @@ -0,0 +1,25 @@ +''' +For N, length of the given strings, + +Time Complexity: O(N) +- first iteration: O(N) +- second iteration: O(N) + +Space Compelxity: O(N) +- dictionaries for each strings: O(N) +''' + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + count_s, count_t = {}, {} + for i in range(len(s)): + count_s[s[i]] = count_s.get(s[i], 0) + 1 + count_t[t[i]] = count_t.get(t[i], 0) + 1 + + for c in count_s: + if c not in count_t or count_s[c] != count_t[c]: + return False + return True From 5e38b0b1075517a098e51ad643f787c32fb90170 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:07:22 +0900 Subject: [PATCH 02/10] Solution: Valid Anagram - rewrite in cpp --- valid-anagram/flynn.cpp | 22 ++++++++++++++++++++++ valid-anagram/flynn.py | 25 ------------------------- 2 files changed, 22 insertions(+), 25 deletions(-) create mode 100644 valid-anagram/flynn.cpp delete mode 100644 valid-anagram/flynn.py diff --git a/valid-anagram/flynn.cpp b/valid-anagram/flynn.cpp new file mode 100644 index 000000000..02d98fa73 --- /dev/null +++ b/valid-anagram/flynn.cpp @@ -0,0 +1,22 @@ +/** + * For length of given strings N, + * + * Time complexity: O(N) + * - iteration for given strings + * + * Space complexity: O(1) + * - the size of the container `count` is constant + */ + +class Solution { +public: + bool isAnagram(string s, string t) { + int count[26] = {0}; + + for (auto c : s) count[c - 'a']++; + for (auto c : t) count[c - 'a']--; + + for (int i = 0; i < 26; i++) if (count[i]) return false; + return true; + } +}; \ No newline at end of file diff --git a/valid-anagram/flynn.py b/valid-anagram/flynn.py deleted file mode 100644 index b45d142a5..000000000 --- a/valid-anagram/flynn.py +++ /dev/null @@ -1,25 +0,0 @@ -''' -For N, length of the given strings, - -Time Complexity: O(N) -- first iteration: O(N) -- second iteration: O(N) - -Space Compelxity: O(N) -- dictionaries for each strings: O(N) -''' - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - count_s, count_t = {}, {} - for i in range(len(s)): - count_s[s[i]] = count_s.get(s[i], 0) + 1 - count_t[t[i]] = count_t.get(t[i], 0) + 1 - - for c in count_s: - if c not in count_t or count_s[c] != count_t[c]: - return False - return True From 0876fb5859ec926f6e93a182c7c453be962176c5 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:14:04 +0900 Subject: [PATCH 03/10] Chore: Valid Anagram - line break --- valid-anagram/flynn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-anagram/flynn.cpp b/valid-anagram/flynn.cpp index 02d98fa73..b6df7007e 100644 --- a/valid-anagram/flynn.cpp +++ b/valid-anagram/flynn.cpp @@ -19,4 +19,4 @@ class Solution { for (int i = 0; i < 26; i++) if (count[i]) return false; return true; } -}; \ No newline at end of file +}; From 6f5a4f17f62e06e8842a9456546c3e8632367c9f Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:38:28 +0900 Subject: [PATCH 04/10] Solution: Counting Bits --- counting-bits/flynn.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 counting-bits/flynn.cpp diff --git a/counting-bits/flynn.cpp b/counting-bits/flynn.cpp new file mode 100644 index 000000000..5067bda98 --- /dev/null +++ b/counting-bits/flynn.cpp @@ -0,0 +1,22 @@ +/** + * Time complexity: O(N) + * + * Space complexity: O(1) + */ + +class Solution { +public: + vector countBits(int n) { + vector res; + res.push_back(0); + + int i = 1, j = 1; + while (i <= n) { + res.push_back(res[i - j] + 1); + i++; + if (i == j * 2) j *= 2; + } + + return res; + } +}; From 6b3f60e87aed825ae84ed590536186b17011c0a8 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 15:49:45 +0900 Subject: [PATCH 05/10] Solution: Encode and Decode Strings --- encode-and-decode-strings/flynn.cpp | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 encode-and-decode-strings/flynn.cpp diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp new file mode 100644 index 000000000..a09391275 --- /dev/null +++ b/encode-and-decode-strings/flynn.cpp @@ -0,0 +1,58 @@ +/** + * Let's say sum of lengths of given strings N, and the length of the longest string M + * + * Encode + * - Time complexity: O(N) + * - Space complexity: O(1) + * + * Decode + * - Time complexity: O(N) + * - Space complexity: O(M) + */ + +class Codec { +public: + + // Encodes a list of strings to a single string. + string encode(vector& strs) { + string res = ""; + for (auto str : strs) { + res += to_string(str.size()); + res.push_back('.'); + res += str; + } + return res; + } + + // Decodes a single string to a list of strings. + vector decode(string s) { + vector res; + int str_size = 0; + string tmp = ""; + + auto it = s.begin(); + while (it != s.end()) { + do { + str_size = str_size * 10 + (*it - '0'); + it++; + } while (*it != '.'); + + it++; + + for (int i = 0; i < str_size; i++) { + tmp.push_back(*it); + it++; + } + + res.push_back(tmp); + str_size = 0; + tmp = ""; + } + + return res; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec codec; +// codec.decode(codec.encode(strs)); From 8efcb4ff809fcbf81de02eb41ad3698ba1145c66 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 22:30:04 +0900 Subject: [PATCH 06/10] Fix: Encode and Decode - Big-O analysis --- encode-and-decode-strings/flynn.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp index a09391275..63305ef40 100644 --- a/encode-and-decode-strings/flynn.cpp +++ b/encode-and-decode-strings/flynn.cpp @@ -1,12 +1,12 @@ /** - * Let's say sum of lengths of given strings N, and the length of the longest string M + * For the number of given strings N, and the length of the longest string M, * * Encode * - Time complexity: O(N) * - Space complexity: O(1) * * Decode - * - Time complexity: O(N) + * - Time complexity: O(NM) * - Space complexity: O(M) */ From 911ee8ece8e3c004e9ec57823b4147f11ba24135 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 14:56:01 +0900 Subject: [PATCH 07/10] Solution: Construct Binary Tree from Preorder and Inorder Traversal --- .../flynn.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp new file mode 100644 index 000000000..9f7593623 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp @@ -0,0 +1,49 @@ +/** + * For the number of given nodes N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) at worst + */ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + unordered_map inorder_index_map; + stack tree_stack; + + for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i; + + TreeNode* root = new TreeNode(preorder[0]); + tree_stack.push(root); + + for (int i = 1; i < preorder.size(); i++) { + TreeNode* curr = new TreeNode(preorder[i]); + + if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) { + tree_stack.top()->left = curr; + } else { + TreeNode* parent; + while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) { + parent = tree_stack.top(); + tree_stack.pop(); + } + parent->right = curr; + } + tree_stack.push(curr); + } + + return root; + } +}; From 8223d78c46bff3b3b72c2d18459eee0ab9b64406 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 15:57:12 +0900 Subject: [PATCH 08/10] Solution: Decode Ways --- decode-ways/flynn.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 decode-ways/flynn.cpp diff --git a/decode-ways/flynn.cpp b/decode-ways/flynn.cpp new file mode 100644 index 000000000..0009f0f31 --- /dev/null +++ b/decode-ways/flynn.cpp @@ -0,0 +1,29 @@ +/** + * For the length of the given string N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) + */ + +class Solution { +public: + int numDecodings(string s) { + if (s[0] == '0') return 0; + + int memo[s.size() + 1]; + + fill(memo, memo + s.size() + 1, 0); + memo[0] = 1; + memo[1] = 1; + + for (int i = 2; i <= s.size(); i++) { + int s_i = i - 1; + if (s[s_i] != '0') memo[i] = memo[i - 1]; + int two_digits = stoi(s.substr(s_i - 1, 2)); + if (10 <= two_digits && two_digits <= 26) memo[i] += memo[i - 2]; + } + + return memo[s.size()]; + } +}; From 6eed7ba9130650906fb05554de64c3f6bcb1dcb9 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 16:39:31 +0900 Subject: [PATCH 09/10] Solution: optimize Decode Ways --- decode-ways/flynn.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/decode-ways/flynn.cpp b/decode-ways/flynn.cpp index 0009f0f31..05d9b6eec 100644 --- a/decode-ways/flynn.cpp +++ b/decode-ways/flynn.cpp @@ -27,3 +27,27 @@ class Solution { return memo[s.size()]; } }; + +/** + * Space complexity O(1) solution + */ + +// class Solution { +// public: +// int numDecodings(string s) { +// if (s[0] == '0') return 0; + +// int one_digit_memo = 1, two_digit_memo = 1; + +// for (int i = 1; i < s.size(); i++) { +// int tmp = 0; +// if (s[i] != '0') tmp = one_digit_memo; +// int two_digits = stoi(s.substr(i - 1, 2)); +// if (10 <= two_digits && two_digits <= 26) tmp += two_digit_memo; +// two_digit_memo = one_digit_memo; +// one_digit_memo = tmp; +// } + +// return one_digit_memo; +// } +// }; From 3abfd93dd063d8be5c2109acadb99c7bb8ec2cbe Mon Sep 17 00:00:00 2001 From: obzva Date: Wed, 21 Aug 2024 15:01:31 +0900 Subject: [PATCH 10/10] Refactor: Encode and Decode Strings --- encode-and-decode-strings/flynn.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp index 63305ef40..955572279 100644 --- a/encode-and-decode-strings/flynn.cpp +++ b/encode-and-decode-strings/flynn.cpp @@ -27,15 +27,16 @@ class Codec { // Decodes a single string to a list of strings. vector decode(string s) { vector res; - int str_size = 0; - string tmp = ""; auto it = s.begin(); while (it != s.end()) { - do { + int str_size = 0; + string tmp = ""; + + while (*it != '.') { str_size = str_size * 10 + (*it - '0'); it++; - } while (*it != '.'); + } it++; @@ -45,8 +46,6 @@ class Codec { } res.push_back(tmp); - str_size = 0; - tmp = ""; } return res;