|
18 | 18 | 15. Intersection of Two Arrays II Easy |
19 | 19 | 16. Sparse Matrix Multiplication Medium |
20 | 20 | 17. Rectangle Overlap Easy |
| 21 | +18. Minimum Window Substring Hard |
| 22 | +19. Valid Number Hard |
21 | 23 |
|
22 | 24 | ## Binary Search |
23 | 25 | 1. Search a 2D Matrix Easy |
|
31 | 33 | 1. Reverse Linked List II Medium |
32 | 34 | 2. Merge K sorted List Medium |
33 | 35 |
|
| 36 | +## String |
| 37 | +1. Valid Anagram Easy |
34 | 38 |
|
35 | 39 |
|
36 | 40 | ## Implementation |
@@ -1079,4 +1083,134 @@ public: |
1079 | 1083 | return (min(r1.x, r2.x) > max(l1.x, l2.x)) && (min(l1.y, l2.y) > max(r1.y, r2.y)); |
1080 | 1084 | } |
1081 | 1085 | }; |
| 1086 | +``` |
| 1087 | +
|
| 1088 | +### **Rectangle Overlap** |
| 1089 | +
|
| 1090 | +***Big O:*** O(K + T) speed, O(1) space, K size of s, T size of T |
| 1091 | +``` |
| 1092 | +Tips: |
| 1093 | + |
| 1094 | +Algorithm |
| 1095 | + |
| 1096 | +We start with two pointers, leftleft and rightright initially pointing to the first element of the string SS. |
| 1097 | + |
| 1098 | +We use the rightright pointer to expand the window until we get a desirable window i.e. a window that contains all of the characters of TT. |
| 1099 | + |
| 1100 | +Once we have a window with all the characters, we can move the left pointer ahead one by one. If the window is still a desirable one we keep on updating the minimum window size. |
| 1101 | + |
| 1102 | +If the window is not desirable any more, we repeat step \; 2step2 onwards. |
| 1103 | +``` |
| 1104 | +```c++ |
| 1105 | +
|
| 1106 | +class Solution { |
| 1107 | +
|
| 1108 | + public: |
| 1109 | + static string minWindow(const string &str, const string &pattern) { |
| 1110 | + // TODO: Write your code here |
| 1111 | + string ret_str = ""; |
| 1112 | + unsigned int min_str = -1, win_s, win_e, match = 0; |
| 1113 | + unordered_map <char, int> umap; |
| 1114 | +
|
| 1115 | + for (auto chr : pattern) |
| 1116 | + umap[chr] ++; |
| 1117 | +
|
| 1118 | + for (win_e = win_s = 0; win_e < str.size(); win_e ++) { |
| 1119 | + if (umap.find(str[win_e]) != umap.end()) { |
| 1120 | + if (--umap[str[win_e]] == 0) { |
| 1121 | + match ++; |
| 1122 | + } |
| 1123 | + } |
| 1124 | + |
| 1125 | + while (match == umap.size()) { |
| 1126 | + if ((win_e - win_s + 1) < min_str) { |
| 1127 | + min_str = min(min_str, win_e - win_s + 1); |
| 1128 | + ret_str = str.substr(win_s, win_e - win_s + 1); |
| 1129 | + } |
| 1130 | + |
| 1131 | + if (umap.find(str[win_s]) != umap.end() && ++umap[str[win_s]] > 0) { |
| 1132 | + match --; |
| 1133 | + } |
| 1134 | +
|
| 1135 | + win_s++; |
| 1136 | + } |
| 1137 | + } |
| 1138 | +
|
| 1139 | + return ret_str; |
| 1140 | + } |
| 1141 | +}; |
| 1142 | +``` |
| 1143 | +### **Valid Number** |
| 1144 | + |
| 1145 | +***Big O:*** O(N) speed, O(1) space |
| 1146 | +``` |
| 1147 | +Tips: |
| 1148 | +
|
| 1149 | +Divide and conquer. Divide it into two parts around the e/E symbol and check both sides. |
| 1150 | +``` |
| 1151 | +```c++ |
| 1152 | +class Solution { |
| 1153 | +public: |
| 1154 | + bool ojbk(string s, int k){ |
| 1155 | + for(int i=0;i<s.size();i++){ |
| 1156 | + if((s[i]=='+' || s[i]=='-') && i!=0) return false; |
| 1157 | + } |
| 1158 | + if(s[0]=='+' || s[0]=='-') s.erase(s.begin()); |
| 1159 | + int countDot=0; |
| 1160 | + for(int i=0;i<s.size();i++){ |
| 1161 | + if(s[i]=='.') countDot++; |
| 1162 | + else if(!isdigit(s[i])) return false; |
| 1163 | + } |
| 1164 | + if(s=="" || s==".") return false; |
| 1165 | + |
| 1166 | + return countDot<=k; |
| 1167 | + } |
| 1168 | + bool isNumber(string s) { |
| 1169 | + while(s.size()>0 && s.back()==' ') s.pop_back(); |
| 1170 | + while(s.size()>0 && s[0]==' ') s.erase(s.begin()); |
| 1171 | + int countE=0; |
| 1172 | + int pos; |
| 1173 | + for(int i=0;i<s.size();i++){ |
| 1174 | + if(s[i]=='e'){ |
| 1175 | + countE++; |
| 1176 | + pos=i; |
| 1177 | + } |
| 1178 | + } |
| 1179 | + if(countE>=2) return false; |
| 1180 | + if(countE==0) return ojbk(s,1); |
| 1181 | + return ojbk(s.substr(0,pos),1) && ojbk(s.substr(pos+1),0); |
| 1182 | + } |
| 1183 | +}; |
| 1184 | +``` |
| 1185 | +
|
| 1186 | +### **Valid Anagram** |
| 1187 | +
|
| 1188 | +***Big O:*** O(N) speed, O(1) space |
| 1189 | +``` |
| 1190 | +Tips: |
| 1191 | + |
| 1192 | +Hash map. |
| 1193 | +``` |
| 1194 | +```c++ |
| 1195 | +class Solution { |
| 1196 | +public: |
| 1197 | + bool isAnagram(string s, string t) { |
| 1198 | + int umap[26] = {}; |
| 1199 | + |
| 1200 | + if (s.size() != t.size()) |
| 1201 | + return false; |
| 1202 | + |
| 1203 | + for (auto &c : s) |
| 1204 | + umap[c-'a'] ++; |
| 1205 | + |
| 1206 | + int count = 0; |
| 1207 | + for (auto &c : t) { |
| 1208 | + umap[c-'a'] --; |
| 1209 | + if (umap[c-'a'] < 0) |
| 1210 | + return false; |
| 1211 | + } |
| 1212 | + |
| 1213 | + return true; |
| 1214 | + } |
| 1215 | +}; |
1082 | 1216 | ``` |
0 commit comments