├── .gitignore ├── Hash Maps ├── 128.LongestConsecutiveSequence.c++ ├── 217.ContainsDuplicate.c++ ├── 238.ProductOfArrayExceptSelf.c++ ├── 242.ValidAnagram.c++ ├── 271.EncodeAndDecodeStrings.c++ ├── 357.TopKFrequentElements.c++ ├── 36.ValidSudoku.c++ └── 49.GroupAnagrams.c++ ├── Simple Arrays └── 1.TwoSum.c++ └── Two pointers ├── 125.ValidPalindrome.c++ └── 167.TwoSum2InputArrayIsSorted.c++ /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe -------------------------------------------------------------------------------- /Hash Maps/128.LongestConsecutiveSequence.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/longest-consecutive-sequence/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | int longestConsecutive(vector& nums) { 8 | // 9 | unordered_map m; 10 | int s = nums.size(); 11 | for (int i = 0; i < s; i++) { 12 | m[nums[i]] = 1; 13 | } 14 | int mx = 0; 15 | for (int i = 0; i < s; i++) { 16 | if (!m[nums[i]-1]) { 17 | int seq = 1; 18 | while(m[nums[i]+seq]) { 19 | seq++; 20 | } 21 | mx = max(mx,seq); 22 | } 23 | } 24 | return mx; 25 | } 26 | }; -------------------------------------------------------------------------------- /Hash Maps/217.ContainsDuplicate.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/contains-duplicate/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | bool containsDuplicate(vector& nums) { 8 | map m; 9 | int size = nums.size(); 10 | while(size--) { 11 | m[nums[size]]++; 12 | if (m[nums[size]] == 2) { 13 | return true; 14 | } 15 | } 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /Hash Maps/238.ProductOfArrayExceptSelf.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/product-of-array-except-self/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | vector productExceptSelf(vector& nums) { 8 | deque pr; 9 | deque pf; 10 | vector ans; 11 | 12 | int s = nums.size(); 13 | pr.push_back(nums[0]); 14 | for (int i = 1; i < s; i++) { 15 | pr.push_back(nums[i] * pr[i-1]); 16 | } 17 | 18 | pf.push_front(nums[s-1]); 19 | for (int i = s-2; i >= 0; i--) { 20 | pf.push_front(nums[i] * pf[0]); 21 | } 22 | 23 | for (int i = 0; i < s; i++) { 24 | if (i == 0) { 25 | ans.push_back(pf[i+1]); 26 | } else if (i == s-1) { 27 | ans.push_back(pr[i-1]); 28 | } else { 29 | ans.push_back(pr[i-1] * pf[i+1]); 30 | } 31 | } 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /Hash Maps/242.ValidAnagram.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/valid-anagram/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | bool isAnagram(string s, string t) { 8 | int l1 = s.length(); 9 | int l2 = t.length(); 10 | if (l1 != l2) return false; 11 | map m; 12 | map p; 13 | while(l1--) { 14 | m[s[l1]]++; 15 | p[t[l1]]++; 16 | } 17 | 18 | for (const auto& i : m) { 19 | if (i.second != p[i.first]) return false; 20 | } 21 | return true; 22 | } 23 | }; -------------------------------------------------------------------------------- /Hash Maps/271.EncodeAndDecodeStrings.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/encode-and-decode-strings/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | string encode(vector& strs) { 8 | string res = ""; 9 | int s = strs.size(); 10 | for (int i = 0; i < s; i++) { 11 | int l = strs[i].length(); 12 | for (int e = 0; e < l; e++) { 13 | res+=strs[i][e]; 14 | } 15 | res+="<"; 16 | } 17 | return res; 18 | } 19 | 20 | vector decode(string s) { 21 | vector res; 22 | int x = 0; 23 | int l = s.length(); 24 | res.push_back(""); 25 | for (int i = 0; i < l; i++) { 26 | if (s[i] == '<') { 27 | x++; 28 | res.push_back(""); 29 | } else { 30 | res[x]+=s[i]; 31 | } 32 | } 33 | res.pop_back(); 34 | return res; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Hash Maps/357.TopKFrequentElements.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/top-k-frequent-elements/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | bool comparePairs(pair a, pair b) { 6 | return a.second > b.second; 7 | } 8 | 9 | class Solution { 10 | public: 11 | vector topKFrequent(vector& nums, int k) { 12 | unordered_map m; 13 | vector> freq; 14 | int s = nums.size(); 15 | while(s--) { 16 | m[nums[s]]++; 17 | } 18 | vector a; 19 | for (auto& i : m) { 20 | freq.push_back(make_pair(i.first, i.second)); 21 | } 22 | 23 | sort(freq.begin(), freq.end(), comparePairs); 24 | 25 | for (int i = 0; i < k; i++) { 26 | a.push_back(freq[i].first); 27 | } 28 | return a; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Hash Maps/36.ValidSudoku.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/valid-sudoku/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | bool isValidSudoku(vector>& board) { 8 | int fl = board.size(); 9 | int cl = board[0].size(); 10 | map> b; 11 | for (int f = 0; f < fl; f++) { 12 | unordered_map m; 13 | unordered_map k; 14 | for (int c = 0; c < cl; c++) { 15 | if (board[f][c] != '.') { 16 | int sg = 3*floor(f/3)+floor(c/3); 17 | b[sg][board[f][c]]++; 18 | if (b[sg][board[f][c]] > 1) { 19 | return false; 20 | } 21 | m[board[f][c]]++; 22 | if (m[board[f][c]] > 1) return false; 23 | } 24 | if (board[c][f] != '.') { 25 | k[board[c][f]]++; 26 | if (k[board[c][f]] > 1) return false; 27 | } 28 | 29 | } 30 | } 31 | return true; 32 | } 33 | }; -------------------------------------------------------------------------------- /Hash Maps/49.GroupAnagrams.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/group-anagrams/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | vector> groupAnagrams(vector& strs) { 8 | map, vector> m; 9 | int s = strs.size(); 10 | while(s--) { 11 | map a; 12 | int ss = strs[s].length(); 13 | while(ss--) { 14 | a[strs[s][ss]]++; 15 | } 16 | m[a].push_back(strs[s]); 17 | } 18 | vector> solution; 19 | for (const auto& i : m) { 20 | int l = i.second.size(); 21 | solution.push_back(i.second); 22 | } 23 | return solution; 24 | } 25 | }; -------------------------------------------------------------------------------- /Simple Arrays/1.TwoSum.c++: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/two-sum/ 2 | // Author: Joan Paneque 3 | // Date: 07/04/2024 4 | 5 | class Solution { 6 | public: 7 | vector twoSum(vector& nums, int target) { 8 | vector solution; 9 | for (int i = 0; i < nums.size(); i++) { 10 | for (int e = 0; e < nums.size(); e++) { 11 | if (e != i) { 12 | if (nums[i]+nums[e] == target) { 13 | solution.push_back(e); 14 | solution.push_back(i); 15 | return solution; 16 | } 17 | } 18 | } 19 | } 20 | return solution; 21 | } 22 | }; -------------------------------------------------------------------------------- /Two pointers/125.ValidPalindrome.c++: -------------------------------------------------------------------------------- 1 | string simplify(string str) { 2 | str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { 3 | return !isalpha(c) && !isdigit(c); 4 | }), str.end()); 5 | transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { 6 | return tolower(c); 7 | }); 8 | 9 | return str; 10 | } 11 | 12 | class Solution { 13 | public: 14 | bool isPalindrome(string s) { 15 | string a = simplify(s); 16 | cout << a; 17 | int l = a.length(); 18 | bool p = true; 19 | for (int i = 0; i < l; i++) { 20 | int e = l-i-1; 21 | if (i > e) break; 22 | if (a[i] != a[e]) return false; 23 | } 24 | return true; 25 | } 26 | }; -------------------------------------------------------------------------------- /Two pointers/167.TwoSum2InputArrayIsSorted.c++: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | int s = numbers.size(); 5 | 6 | int p1 = 0; 7 | int p2 = s-1; 8 | 9 | while(1) { 10 | if (numbers[p1] + numbers[p2] > target) { 11 | p2--; 12 | } else if (numbers[p1] + numbers[p2] < target) { 13 | p1++; 14 | } else { 15 | return { p1+1, p2+1 }; 16 | } 17 | } 18 | 19 | return { 1 }; 20 | } 21 | }; --------------------------------------------------------------------------------