This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| string reverseString(string s) { | |
| int l = s.length(); | |
| for(int i = 0; i < l/2; i++){ | |
| char c = s[i]; | |
| s[i] = s[l-1-i]; | |
| s[l-1-i] = c; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <vector> | |
| #include <iostream> | |
| #include <sstream> | |
| using namespace std; | |
| string stripKDigits(string num, int k){ | |
| stringstream out; | |
| int i = 0; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int>> subsets(vector<int>& nums) { | |
| vector<vector<int>> powerset; | |
| powerset.push_back(vector<int>(0)); | |
| // after each iteration of this loop we are left with the powerset of the subset nums[0..i] | |
| for(int i = 0; i < nums.size(); i++){ | |
| // append nums[i] to already recorded subsets to form new ones. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| private: | |
| vector<vector<int>> powerset; // stores all subsets | |
| vector<int> subset; // temporary subset which will be updated as the recursive function executes | |
| public: | |
| vector<vector<int>> subsets(vector<int>& nums) { | |
| backtrack(nums, 0); | |
| return powerset; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| bool checkInclusion(string s1, string s2) { | |
| unordered_map<char, int> table; | |
| for(char c : s1){ | |
| table[c]++; | |
| } | |
| int begin = 0, end = 0, counter = table.size(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int lengthOfLongestSubstringTwoDistinct(string s) { | |
| if(s.length() == 0) return 0; | |
| unordered_map<char, int> table; | |
| int begin = 0, end = 0, len = 0, counter = 0; | |
| while(end < s.length()){ | |
| char current = s[end]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<int> findSubstring(string s, vector<string>& words) { | |
| unordered_map<string, int> table; | |
| vector<int> ans; | |
| if(words.size() == 0) return ans; | |
| int window_size = 0; | |
| int word_size = words[0].length(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<int> findAnagrams(string s, string p) { | |
| unordered_map<char, int> table; | |
| vector<int> ans; | |
| for(char c : p){ | |
| table[c]++; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int lengthOfLongestSubstring(string s) { | |
| unordered_map<char, int> seen; | |
| int begin = 0, end = 0; | |
| int len = 0; | |
| string ans = ""; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| string minWindow(string s, string t) { | |
| unordered_map<char, int> table; | |
| // initialize frequency table for t | |
| for(char c : t){ | |
| table[c]++; | |
| } | |