diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/heozeop.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/heozeop.cpp new file mode 100644 index 000000000..a4ece08fa --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/heozeop.cpp @@ -0,0 +1,37 @@ +// Time Complexity: O(n^2) +// Spatial Complexity: O(n) + +class Solution { +private: + int findIndex(int targetVal, vector& inorder) { + auto pos = find(inorder.begin(), inorder.end(), targetVal); + if (pos == inorder.end()) { + return -1; + } + + return pos - inorder.begin(); + } + + TreeNode* dfs(vector& preorder, vector& inorder, int preorderIndex, int startIndex, int endIndex) { + if (preorder.size() <= preorderIndex || startIndex > endIndex) { + return nullptr; + } + + int targetValue = preorder[preorderIndex]; + int rootIndex = this->findIndex(targetValue, inorder); + if(rootIndex < 0) { + return nullptr; + } + + int leftSubtreeLength = rootIndex - startIndex; + + TreeNode* left = dfs(preorder, inorder, preorderIndex + 1, startIndex, rootIndex - 1); + TreeNode* right = dfs(preorder, inorder, preorderIndex + 1 + leftSubtreeLength, rootIndex + 1, endIndex); + + return new TreeNode(targetValue, left, right); + } +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + return this->dfs(preorder, inorder, 0, 0, preorder.size() - 1); + } +}; diff --git a/counting-bits/heozeop.cpp b/counting-bits/heozeop.cpp new file mode 100644 index 000000000..508075ced --- /dev/null +++ b/counting-bits/heozeop.cpp @@ -0,0 +1,31 @@ +// Time Complexity: O(nlogn) +// Spatial Complexity: O(n) + +class Solution { +private: + int count1(int n) { + int ans = 0; + + while(n) { + if (n % 2) { + ++ans; + } + + n /= 2; + } + + return ans; + } + +public: + vector countBits(int n) { + vector ans(n + 1); + + for(int i = 0; i <= n; ++i) { + ans[i] = this->count1(i); + } + + return ans; + } +}; + diff --git a/decode-ways/heozeop.cpp b/decode-ways/heozeop.cpp new file mode 100644 index 000000000..4654ca568 --- /dev/null +++ b/decode-ways/heozeop.cpp @@ -0,0 +1,31 @@ +// Time Complexity: O(n) +// Spatial Complexity: O(n) + +class Solution { +public: + int numDecodings(string s) { + if(s.length() < 1 || s[0] == '0') { + return 0; + } + + vector dp(s.length() + 1, 0); + dp[0] = dp[1] = 1; + if(s[1] == '0') { + dp[1] = 0; + } + + int prev,pprev; + for(int i = 2; i <= s.length(); ++i) { + prev = s[i - 1] - '0'; + if (prev <= 9 && prev > 0) { + dp[i] += dp[i-1]; + } + pprev = (s[i - 2] - '0') * 10 + prev; + if(pprev <= 26 && pprev > 9) { + dp[i] += dp[i-2]; + } + } + + return dp[s.length()]; + } +}; diff --git a/encode-and-decode-strings/heozeop.cpp b/encode-and-decode-strings/heozeop.cpp new file mode 100644 index 000000000..5762a4ee5 --- /dev/null +++ b/encode-and-decode-strings/heozeop.cpp @@ -0,0 +1,55 @@ +// Time Complexity: O(n) +// Spatial Complexity: O(n) + +class Codec { +private: + string genereateRandomString(size_t size) { + string randomString = ""; + for(size_t i = 0; i < size; ++i) { + randomString += static_cast(rand() % 256); + } + + return randomString; + } + + vector split(string target, string delimiter) { + vector ans; + + int delimiterLength = delimiter.size(); + size_t pos = target.find(delimiter); + while(pos != string::npos) { + ans.push_back(target.substr(0, pos)); + + target = target.substr(pos + delimiterLength, target.size()); + pos = target.find(delimiter); + } + ans.push_back(target); + + return ans; + } + + string delimiter = this->genereateRandomString(10); + +public: + + // Encodes a list of strings to a single string. + string encode(vector& strs) { + string encodedString = strs[0]; + + for(int i = 1; i < strs.size(); ++i) { + encodedString += this->delimiter + strs[i]; + } + + return encodedString; + } + + // Decodes a single string to a list of strings. + vector decode(string s) { + + return split(s, this->delimiter); + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec codec; +// codec.decode(codec.encode(strs)); diff --git a/valid-anagram/heozeop.cpp b/valid-anagram/heozeop.cpp new file mode 100644 index 000000000..b62f78d77 --- /dev/null +++ b/valid-anagram/heozeop.cpp @@ -0,0 +1,26 @@ +// Time Complexity: O(n) +// Spatial Complexity: O(1) + +class Solution { +public: + bool isAnagram(string s, string t) { + int numberOfAlphabet[26]; + + for(char character : s) { + numberOfAlphabet[character - 'a']++; + } + + for(char character : t) { + numberOfAlphabet[character - 'a']--; + } + + for(int i = 0; i < 26; ++i) { + if (numberOfAlphabet[i] != 0) { + return false; + } + } + + return true; + } +}; +