├── .DS_Store ├── Array & Hashing ├── .DS_Store ├── is-subsequence.cpp ├── Remove Element.cpp ├── replace-elements-with-greatest-element-on-right-side.cpp ├── Contains Duplicate-Leetcode-217.cpp ├── Maximum Number of Balloons.cpp ├── length-of-last-word.cpp ├── Find Pivot Index.cpp ├── Pascal's Triangle.cpp ├── Can Place Flowers.cpp ├── Isomorphic Strings.cpp ├── Longest Common Prefix.cpp ├── Subarray Sum Equals K.cpp ├── Sort Colors.cpp ├── Group Anagrams.cpp ├── Longest Consecutive Sequence.cpp ├── Find All Numbers Disappeared in an Array.cpp ├── Two-sum.cpp ├── Majority Element.cpp ├── Repeated DNA Sequences.cpp ├── Encode and Decode TinyURL.cpp ├── Largest Number.cpp ├── Unique Email Addresses.cpp ├── Top K Frequent Elements.cpp ├── Best Time to Buy and Sell Stock II.cpp ├── Find All Anagrams in a String.cpp ├── Valid Sudoku.cpp ├── Word Pattern.cpp ├── Product of Array Except Self.cpp └── Unique Length-3 Palindromic Subsequences.cpp ├── README.md └── Two Pointer ├── Reverse String.cpp ├── Move Zeroes.cpp ├── Container With Most Water.cpp ├── Valid Palindrome.cpp ├── Merge Sorted Array.cpp ├── Two Sum II - Input Array Is Sorted Medium 8.4K 1.1K.cpp ├── Valid Palindrome II.cpp └── 3Sum.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmed-tahoon/LeetCode-Solutions/HEAD/.DS_Store -------------------------------------------------------------------------------- /Array & Hashing/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmed-tahoon/LeetCode-Solutions/HEAD/Array & Hashing/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode-Solutions 2 | 3 | 4 | videos on YouTube : https://www.youtube.com/playlist?list=PL9B5WmPowtSlahISh8MilDLUneW8JTaOz 5 | -------------------------------------------------------------------------------- /Two Pointer/Reverse String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | 5 | int l=0; 6 | int r=s.size()-1; 7 | 8 | 9 | while(l<=r) 10 | { 11 | swap(s[l],s[r]); 12 | l++; 13 | r--; 14 | } 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /Array & Hashing/is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int j=0; 5 | for(int i =0 ; i< t.size() ; i++) 6 | { 7 | if(t[i]==s[j]) 8 | { 9 | j++; 10 | } 11 | } 12 | 13 | return j==s.size(); 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /Array & Hashing/Remove Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int removeElement(vector &nums, int val) 5 | { 6 | 7 | int x = 0; 8 | 9 | for (int j = 0; j < nums.size(); j++) 10 | { 11 | 12 | if (nums[j] == val) 13 | x++; 14 | } 15 | return nums.size() - x; 16 | } 17 | }; -------------------------------------------------------------------------------- /Two Pointer/Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int l =0; 5 | for(int i =0; i< nums.size();i++) 6 | { 7 | if(nums[i]!=0) 8 | { 9 | nums[l]=nums[i]; 10 | l++; 11 | } 12 | } 13 | 14 | for(int i =l ; i replaceElements(vector& arr) { 4 | 5 | 6 | int mx=-1; 7 | 8 | for(int i = arr.size()-1 ; i>=0; i--) 9 | { 10 | int n = arr[i]; 11 | arr[i]=mx; 12 | mx=max(n,mx); 13 | } 14 | 15 | 16 | return arr; 17 | 18 | 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /Array & Hashing/Contains Duplicate-Leetcode-217.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | 5 | mapmp; 6 | 7 | for(int i =0 ;i < nums.size(); i++) 8 | { 9 | if(mp[nums[i]]) 10 | { 11 | return true; 12 | } 13 | 14 | mp[nums[i]]=true; 15 | 16 | } 17 | 18 | return false; 19 | } 20 | 21 | }; 22 | 23 | 24 | // Time : o(n) 25 | //Space : o(n) -------------------------------------------------------------------------------- /Array & Hashing/Maximum Number of Balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxNumberOfBalloons(string text) 5 | { 6 | 7 | map mp; 8 | 9 | for (int i = 0; i < text.size(); i++) 10 | mp[text[i]]++; 11 | 12 | int b = mp['b']; 13 | int a = mp['a']; 14 | int l = mp['l'] / 2; 15 | int o = mp['o'] / 2; 16 | int n = mp['n']; 17 | 18 | return min({a, b, l, o, n}); 19 | } 20 | }; -------------------------------------------------------------------------------- /Array & Hashing/length-of-last-word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | 5 | 6 | int len=0; 7 | for(int i = s.size()-1 ; i>=0 ; i--) 8 | { 9 | 10 | if(len!=0 &&s[i]==' ') 11 | return len; 12 | 13 | if(s[i]==' ') 14 | continue; 15 | else 16 | { 17 | len++; 18 | } 19 | } 20 | 21 | 22 | return len; 23 | 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Array & Hashing/Find Pivot Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int pivotIndex(vector &nums) 5 | { 6 | 7 | int rightSum = accumulate(nums.begin(), nums.end(), 0); 8 | int leftSum = 0; 9 | 10 | for (int i = 0; i < nums.size(); i++) 11 | { 12 | rightSum -= nums[i]; 13 | if (rightSum == leftSum) 14 | return i; 15 | 16 | leftSum += nums[i]; 17 | } 18 | 19 | return -1; 20 | } 21 | }; -------------------------------------------------------------------------------- /Array & Hashing/Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector> generate(int numRows) 5 | { 6 | 7 | vector> ans; 8 | 9 | for (int i = 0; i < numRows; i++) 10 | { 11 | vector row(i + 1, 1); 12 | for (int j = 1; j < i; j++) 13 | { 14 | row[j] = ans[i - 1][j - 1] + ans[i - 1][j]; 15 | } 16 | ans.push_back(row); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Array & Hashing/Can Place Flowers.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool canPlaceFlowers(vector &flowerbed, int n) 5 | { 6 | 7 | flowerbed.insert(flowerbed.begin(), 0); 8 | 9 | flowerbed.push_back(0); 10 | 11 | for (int i = 1; i < flowerbed.size() - 1; i++) 12 | { 13 | if (flowerbed[i - 1] + flowerbed[i] + flowerbed[i + 1] == 0) 14 | { 15 | n--; 16 | i++; 17 | } 18 | } 19 | 20 | return n <= 0; 21 | } 22 | }; -------------------------------------------------------------------------------- /Array & Hashing/Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isIsomorphic(string s, string t) 5 | { 6 | 7 | map mp1; 8 | map mp2; 9 | 10 | for (int i = 0; i < s.size(); i++) 11 | { 12 | if (mp1[s[i]] && mp1[s[i]] != t[i] || mp2[t[i]] && mp2[t[i]] != s[i]) 13 | { 14 | return false; 15 | } 16 | 17 | mp1[s[i]] = t[i]; 18 | mp2[t[i]] = s[i]; 19 | } 20 | 21 | return true; 22 | } 23 | }; -------------------------------------------------------------------------------- /Array & Hashing/Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string longestCommonPrefix(vector &strs) 5 | { 6 | 7 | string ans = ""; 8 | 9 | for (int i = 0; i < strs[0].size(); i++) 10 | { 11 | char x = strs[0][i]; // flower 12 | 13 | for (int j = 0; j < strs.size(); j++) 14 | { 15 | if (strs[j][i] != x) 16 | return ans; 17 | } 18 | ans += x; 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Two Pointer/ Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int st=0; 5 | int end=height.size()-1; 6 | int mx=INT_MIN; 7 | while(st &nums, int k) 5 | { 6 | 7 | map mp; 8 | 9 | mp[0] = 1; 10 | int sum = 0; 11 | int ans = 0; 12 | for (int i = 0; i < nums.size(); i++) 13 | { 14 | sum += nums[i]; 15 | int f = sum - k; 16 | 17 | if (mp.find(f) != mp.end()) 18 | { 19 | ans += mp[f]; 20 | } 21 | 22 | mp[sum]++; 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Array & Hashing/Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void sortColors(vector &nums) 5 | { 6 | 7 | int f = 0; 8 | int l = nums.size() - 1; 9 | 10 | for (int i = 0; i <= l; i++) 11 | { 12 | if (nums[i] == 0) 13 | { 14 | swap(nums[i], nums[f]); 15 | f++; 16 | } 17 | 18 | else if (nums[i] == 2) 19 | { 20 | swap(nums[i], nums[l]); 21 | i--; 22 | l--; 23 | } 24 | } 25 | } 26 | }; -------------------------------------------------------------------------------- /Array & Hashing/Group Anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector> groupAnagrams(vector &strs) 5 | { 6 | 7 | map> mp; 8 | 9 | for (int i = 0; i < strs.size(); i++) 10 | { 11 | string x = strs[i]; 12 | 13 | sort(x.begin(), x.end()); 14 | 15 | mp[x].push_back(strs[i]); 16 | } 17 | 18 | vector> ans; 19 | 20 | for (auto it : mp) 21 | { 22 | ans.push_back(it.second); 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Two Pointer/Valid Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | 5 | int l=0; 6 | int r=s.size()-1; 7 | 8 | while(l<=r) 9 | { 10 | if(!isalnum(s[l])) 11 | { 12 | l++; 13 | } 14 | else if(!isalnum(s[r])) 15 | { 16 | r--; 17 | } 18 | else if(tolower(s[l])!=tolower(s[r])) 19 | { 20 | return false; 21 | } 22 | else{ 23 | l++; 24 | r--; 25 | } 26 | } 27 | 28 | return true; 29 | 30 | } 31 | }; -------------------------------------------------------------------------------- /Array & Hashing/Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int longestConsecutive(vector &nums) 5 | { 6 | 7 | unordered_set st(nums.begin(), nums.end()); 8 | int mx = 0; 9 | 10 | for (int i = 0; i < nums.size(); i++) 11 | { 12 | if (!st.count(nums[i] - 1)) 13 | { 14 | int len = 1; 15 | 16 | while (st.count(nums[i] + len)) 17 | len++; 18 | 19 | mx = max(mx, len); 20 | } 21 | } 22 | 23 | return mx; 24 | } 25 | }; -------------------------------------------------------------------------------- /Array & Hashing/Find All Numbers Disappeared in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector findDisappearedNumbers(vector &nums) 5 | { 6 | 7 | vector ans; 8 | 9 | for (int i = 0; i < nums.size(); i++) 10 | { 11 | int x = abs(nums[i]) - 1; 12 | 13 | if (nums[x] > 0) 14 | nums[x] = -nums[x]; 15 | } 16 | 17 | for (int i = 0; i < nums.size(); i++) 18 | { 19 | if (nums[i] > 0) 20 | ans.push_back(i + 1); 21 | } 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Array & Hashing/Two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | 5 | 6 | vectorv; 7 | 8 | for(int i =0 ; i & nums1, int m, vector& nums2, int n) { 4 | int i = m-1; 5 | int j = n-1; 6 | 7 | int tot = n+m-1; 8 | 9 | while(i>=0 && j>=0) 10 | { 11 | if(nums1[i]=0) 21 | { 22 | nums1[tot--]=nums2[j--]; 23 | } 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Two Pointer/Two Sum II - Input Array Is Sorted Medium 8.4K 1.1K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | 5 | int st=0; 6 | int end=numbers.size()-1; 7 | 8 | while(st<=end) 9 | { 10 | if(numbers[st]+numbers[end]==target) 11 | { 12 | return {st+1,end+1}; 13 | } 14 | else if(numbers[st]+numbers[end]>target) 15 | { 16 | end--; 17 | } 18 | else 19 | st++; 20 | } 21 | return {-1,-1}; 22 | } 23 | }; -------------------------------------------------------------------------------- /Array & Hashing/Majority Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int majorityElement(vector &nums) 5 | { 6 | 7 | sort(nums.begin(), nums.end()); 8 | 9 | return nums[nums.size() / 2]; 10 | } 11 | }; 12 | /// @brief ////////////////////////////////////////////////// 13 | class Solution 14 | { 15 | public: 16 | int majorityElement(vector &nums) 17 | { 18 | map mp; 19 | 20 | for (int i = 0; i < nums.size(); i++) 21 | { 22 | if (mp[nums[i]] >= nums.size() / 2) 23 | return nums[i]; 24 | mp[nums[i]]++; 25 | } 26 | 27 | return 0; 28 | } 29 | }; -------------------------------------------------------------------------------- /Array & Hashing/Repeated DNA Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector findRepeatedDnaSequences(string s) 5 | { 6 | 7 | map mp; 8 | 9 | vector ans; 10 | string temp = ""; 11 | for (int i = 0; i < s.size(); i++) 12 | { 13 | temp += s[i]; 14 | if (temp.size() == 10) 15 | { 16 | if (mp[temp] == 1) 17 | { 18 | ans.push_back(temp); 19 | } 20 | 21 | mp[temp]++; 22 | 23 | temp = temp.substr(1, temp.size()); 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /Array & Hashing/Encode and Decode TinyURL.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | map mp; 5 | int start = 1; 6 | string base = "http://flkdsjfkl.com/4"; 7 | 8 | // Encodes a URL to a shortened URL. 9 | string encode(string longUrl) 10 | { 11 | 12 | string res = base + to_string(start); 13 | start++; 14 | mp[res] = longUrl; 15 | return res; 16 | } 17 | 18 | // Decodes a shortened URL to its original URL. 19 | string decode(string shortUrl) 20 | { 21 | return mp[shortUrl]; 22 | } 23 | }; 24 | 25 | // Your Solution object will be instantiated and called as such: 26 | // Solution solution; 27 | // solution.decode(solution.encode(url)); -------------------------------------------------------------------------------- /Array & Hashing/Largest Number.cpp: -------------------------------------------------------------------------------- 1 | 2 | bool com(string s1, string s2) 3 | { 4 | return s1 + s2 > s2 + s1; 5 | } 6 | class Solution 7 | { 8 | public: 9 | string largestNumber(vector &nums) 10 | { 11 | 12 | string new_num[nums.size()]; 13 | 14 | for (int i = 0; i < nums.size(); i++) 15 | { 16 | new_num[i] = to_string(nums[i]); 17 | } 18 | 19 | sort(new_num, new_num + nums.size(), com); // nlogn 20 | 21 | string ans = ""; 22 | 23 | for (int i = 0; i < nums.size(); i++) 24 | { 25 | ans += new_num[i]; 26 | } 27 | 28 | if (new_num[0] == "0") 29 | return "0"; 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /Two Pointer/Valid Palindrome II.cpp: -------------------------------------------------------------------------------- 1 | //Solution 01: 2 | class Solution { 3 | public: 4 | 5 | bool ispli(string s , int l , int r) 6 | { 7 | while(l<=r) 8 | { 9 | if(s[l]!=s[r]) 10 | return false; 11 | 12 | l++; 13 | r--; 14 | } 15 | 16 | return true; 17 | } 18 | 19 | 20 | bool validPalindrome(string s) { 21 | int l=0; 22 | int r=s.size()-1; 23 | while(l<=r) 24 | { 25 | if(s[l]==s[r]) 26 | { 27 | l++; 28 | r--; 29 | } 30 | else 31 | { 32 | return (ispli(s,l+1,r)||ispli(s,l,r-1)); 33 | } 34 | } 35 | 36 | return true; 37 | } 38 | }; -------------------------------------------------------------------------------- /Array & Hashing/Unique Email Addresses.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int numUniqueEmails(vector &emails) 5 | { 6 | 7 | set st; 8 | 9 | for (int i = 0; i < emails.size(); i++) 10 | { 11 | string email = emails[i]; 12 | string cleanEmail = ""; 13 | for (int j = 0; j < email.size(); j++) 14 | { 15 | if (email[j] == '.') 16 | continue; 17 | else if (email[j] == '+' || email[j] == '@') 18 | break; 19 | cleanEmail += email[j]; 20 | } 21 | 22 | cleanEmail += email.substr(email.find('@'), email.size() - 1); 23 | st.insert(cleanEmail); 24 | } 25 | 26 | return st.size(); 27 | } 28 | }; -------------------------------------------------------------------------------- /Array & Hashing/Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector topKFrequent(vector &nums, int k) 5 | { 6 | int n = nums.size(); 7 | 8 | map mp; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | mp[nums[i]]++; 13 | } 14 | 15 | vector> buckets(n + 1); 16 | 17 | for (auto it : mp) 18 | { 19 | buckets[it.second].push_back(it.first); 20 | } 21 | 22 | vector ans; 23 | 24 | for (int i = n; i >= 0; i--) 25 | { 26 | if (ans.size() >= k) 27 | { 28 | break; 29 | } 30 | ans.insert(ans.begin(), buckets[i].begin(), buckets[i].end()); 31 | } 32 | 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /Array & Hashing/Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////Code 1//////////////////////// 2 | class Solution 3 | { 4 | public: 5 | int maxProfit(vector &prices) 6 | { 7 | 8 | int mx = 0; 9 | 10 | for (int i = 1; i < prices.size(); i++) 11 | { 12 | 13 | if (prices[i] > prices[i - 1]) 14 | { 15 | mx += prices[i] - prices[i - 1]; 16 | } 17 | } 18 | 19 | return mx; 20 | } 21 | }; 22 | ///////////////////////////////Code 2//////////////////////// 23 | class Solution 24 | { 25 | public: 26 | int maxProfit(vector &prices) 27 | { 28 | 29 | int mx = 0; 30 | 31 | for (int i = 1; i < prices.size(); i++) 32 | { 33 | 34 | mx += max(prices[i] - prices[i - 1], 0); 35 | } 36 | 37 | return mx; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /Array & Hashing/Find All Anagrams in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector findAnagrams(string s, string p) 5 | { 6 | 7 | int s_len = s.length(); 8 | int p_len = p.length(); 9 | 10 | if (s.size() < p.size()) 11 | return {}; 12 | 13 | vector vp(26, 0); 14 | vector vs(26, 0); 15 | 16 | vector ans; 17 | 18 | for (int i = 0; i < p_len; i++) 19 | { 20 | vp[p[i] - 'a']++; 21 | } 22 | 23 | int start = 0; 24 | 25 | for (int i = 0; i < s_len; i++) 26 | { 27 | 28 | vs[s[i] - 'a']++; 29 | 30 | if (i >= p.size()) 31 | { 32 | vs[s[start] - 'a']--; 33 | start++; 34 | } 35 | 36 | if (vp == vs) 37 | ans.push_back(start); 38 | } 39 | 40 | return ans; 41 | } 42 | }; -------------------------------------------------------------------------------- /Array & Hashing/Valid Sudoku.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isValidSudoku(vector> &board) 5 | { 6 | 7 | vector> rows(9); 8 | vector> cols(9); 9 | int blocks[3][3][10] = {0}; 10 | 11 | for (int i = 0; i < 9; i++) 12 | { 13 | for (int j = 0; j < 9; j++) 14 | { 15 | 16 | if (board[i][j] == '.') 17 | { 18 | continue; 19 | } 20 | 21 | int curr = board[i][j] - '0'; 22 | 23 | if (rows[i].count(curr) || cols[j].count(curr) || 24 | blocks[i / 3][j / 3][curr] != 0) 25 | { 26 | return false; 27 | } 28 | 29 | rows[i].insert(curr); 30 | cols[j].insert(curr); 31 | blocks[i / 3][j / 3][curr] = curr; 32 | } 33 | } 34 | return true; 35 | } 36 | }; -------------------------------------------------------------------------------- /Array & Hashing/Word Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool wordPattern(string pattern, string s) 5 | { 6 | 7 | map charToWord; 8 | map WordToChar; 9 | 10 | vector v; 11 | // word variable to store word in the string s 12 | string word; 13 | // making a string stream 14 | stringstream iss(s); 15 | // Push each word in vector 16 | while (iss >> word) 17 | v.push_back(word); 18 | 19 | if (v.size() != pattern.size()) 20 | return false; 21 | 22 | for (int c = 0; c < pattern.size(); c++) 23 | { 24 | string word = v[c]; 25 | if (charToWord[pattern[c]] != "" && charToWord[pattern[c]] != word) 26 | return false; 27 | if (WordToChar[word] && WordToChar[word] != pattern[c]) 28 | return false; 29 | 30 | charToWord[pattern[c]] = word; 31 | WordToChar[word] = pattern[c]; 32 | } 33 | return true; // both the lengths should be equal 34 | } 35 | }; -------------------------------------------------------------------------------- /Array & Hashing/Product of Array Except Self.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector productExceptSelf(vector &nums) 5 | { 6 | int post[100005] = {1}; 7 | int pre[100005] = {1}; 8 | 9 | post[nums.size()] = 1; 10 | 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | pre[i + 1] = pre[i] * nums[i]; 14 | } 15 | 16 | for (int i = nums.size() - 1; i >= 0; i--) 17 | { 18 | post[i] = post[i + 1] * nums[i]; 19 | } 20 | 21 | vector ans; 22 | 23 | for (int i = 0; i < nums.size(); i++) 24 | { 25 | ans.push_back(pre[i] * post[i + 1]); 26 | } 27 | 28 | return ans; 29 | } 30 | }; 31 | 32 | 33 | 34 | 35 | 36 | 37 | ////////////////////////////////////////////Sol 2///////////////////// 38 | 39 | class Solution 40 | { 41 | public: 42 | vector productExceptSelf(vector &nums) 43 | { 44 | 45 | int n = nums.size(); 46 | 47 | vector res(n, 1); 48 | 49 | // prefix 50 | 51 | int pre = 1; 52 | 53 | for (int i = 0; i < n; i++) 54 | { 55 | res[i] = pre; 56 | pre = pre * nums[i]; 57 | } 58 | int pos = 1; 59 | for (int i = n - 1; i >= 0; i--) 60 | { 61 | res[i] = res[i] * pos; 62 | pos = pos * nums[i]; 63 | } 64 | 65 | return res; 66 | } 67 | }; -------------------------------------------------------------------------------- /Two Pointer/3Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | 7 | vector >v1; 8 | 9 | for(int i =0; i< nums.size(); i++) 10 | { 11 | if(i>0&& nums[i]==nums[i-1]) 12 | continue; 13 | 14 | int num1=nums[i]; 15 | 16 | int st=i+1; 17 | int end=nums.size()-1; 18 | while(st st; 19 | if (first[i] < last[i]) 20 | { 21 | for (int j = first[i] + 1; j < last[i]; j++) 22 | { 23 | st.insert(s[j]); 24 | } 25 | res += st.size(); 26 | } 27 | } 28 | return res; 29 | } 30 | }; 31 | 32 | ///////////////////////////////Code 2//////////////////////// 33 | class Solution 34 | { 35 | public: 36 | int countPalindromicSubsequence(string s) 37 | { 38 | 39 | int first[26] = {[0 ... 25] = INT_MAX}, last[26] = {0}, res = 0; 40 | 41 | for (int i = 0; i < s.size(); i++) 42 | { 43 | first[s[i] - 'a'] = min(i, first[s[i] - 'a']); 44 | last[s[i] - 'a'] = i; 45 | } 46 | 47 | for (int i = 0; i < 26; i++) 48 | { 49 | if (first[i] < last[i]) 50 | { 51 | res += unordered_set(begin(s) + first[i] + 1, begin(s) + last[i]).size(); 52 | } 53 | } 54 | return res; 55 | } 56 | }; --------------------------------------------------------------------------------