1. 3Sum

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/776/

test all paths (time limit exceeded):

  1. class Solution {
  2. public:
  3. vector<vector<int>> threeSum(vector<int>& nums) {
  4. int n = nums.size();
  5. if(n <= 2)
  6. return {};
  7. vector<vector<int>> result;
  8. vector<int> curr = {};
  9. threeSum(nums, result, curr, 0);
  10. for(int i = 0; i < result.size(); i++){
  11. sort(result[i].begin(), result[i].end());
  12. }
  13. sort(result.begin(), result.end());
  14. result.erase(unique(result.begin(), result.end()), result.end());
  15. return result;
  16. }
  17. private:
  18. void threeSum(vector<int>& nums, vector<vector<int>>& result, vector<int>& curr, int index){
  19. if(curr.size() == 3){
  20. if(curr[0] + curr[1] + curr[2] == 0){
  21. result.push_back(curr);
  22. }
  23. return;
  24. }
  25. for(int i = index; i < (nums.size() - (2 - curr.size())); i++){
  26. curr.push_back(nums[i]);
  27. threeSum(nums, result, curr, (i + 1));
  28. curr.pop_back();
  29. }
  30. }
  31. };

2 pointers:

  1. class Solution {
  2. public:
  3. vector<vector<int>> threeSum(vector<int>& nums) {
  4. int n = nums.size();
  5. if(n <= 2) //if length <= 2, cannot get 3 numbers
  6. return {};
  7. vector<vector<int>> result;
  8. sort(nums.begin(), nums.end()); //sort in ascending order
  9. int l;
  10. int r;
  11. for(int i = 0; i < n - 2; i++){ //use 3 indexes, i l r, the rightest i is the 3rd last one
  12. if(nums[i] > 0) //if nums[i] > 0, it means nums[l] > 0 and nums[r] > 0
  13. break;
  14. if (i > 0 && nums[i] == nums[i - 1]) //skip duplicates
  15. continue;
  16. l = i + 1;
  17. r = n - 1;
  18. while (l < r) {
  19. if (nums[i] + nums[l] + nums[r] == 0) {
  20. result.push_back({nums[i], nums[l], nums[r]});
  21. l++;
  22. r--;
  23. while (l < r && nums[l] == nums[l - 1]) //skip duplicates
  24. l++;
  25. while (l < r && nums[r] == nums[r + 1]) //skip duplicates
  26. r--;
  27. } else if (nums[i] + nums[l] + nums[r] < 0) { // adjust l
  28. l++;
  29. } else { // adjust r
  30. r--;
  31. }
  32. }
  33. }
  34. return result;
  35. }
  36. };

2. Set Matrix Zeroes

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/777/

use the first row/col as an indicator:

  1. class Solution {
  2. public:
  3. void setZeroes(vector<vector<int>>& matrix) {
  4. const int m = matrix.size();
  5. const int n = matrix[0].size();
  6. bool frow_set_0 = false;
  7. bool fcol_set_0 = false;
  8. for(int r = 0; r < m; r++){ //dicide to set the first row into 0
  9. if(matrix[r][0]==0){
  10. fcol_set_0 = true;
  11. break;
  12. }
  13. }
  14. for(int c = 0; c < n; c++){ //dicide to set the first column into 0
  15. if(matrix[0][c]==0){
  16. frow_set_0 = true;
  17. break;
  18. }
  19. }
  20. for(int r = 0; r < m; r++){
  21. for(int c = 0; c < n; c++){
  22. if(matrix[r][c]==0){ //use corresponding grids in the first row/col as indicators
  23. matrix[r][0] = 0;
  24. matrix[0][c] = 0;
  25. }
  26. }
  27. }
  28. for(int r = 1; r < m; r++){ //set rows
  29. if(matrix[r][0]==0){
  30. for(int c = 1; c < n; c++){
  31. matrix[r][c] = 0;
  32. }
  33. }
  34. }
  35. for(int c = 1; c < n; c++){ //set columns
  36. if(matrix[0][c]==0){
  37. for(int r = 1; r < m; r++){
  38. matrix[r][c] = 0;
  39. }
  40. }
  41. }
  42. if(frow_set_0){ //set the first row
  43. for(int c = 0; c < n; c++){
  44. matrix[0][c] = 0;
  45. }
  46. }
  47. if(fcol_set_0){ //set the first column
  48. for(int r = 0; r < m; r++){
  49. matrix[r][0] = 0;
  50. }
  51. }
  52. }
  53. };

3. Group Anagrams

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/778/

copy the vector and organize:

  1. class Solution {
  2. public:
  3. vector<vector<string>> groupAnagrams(vector<string>& strs) {
  4. vector<vector<string>> result;
  5. if(strs.size()==1){ //if only one category
  6. result.push_back(strs);
  7. return result;
  8. }
  9. int n = strs.size();
  10. vector<string> copy = strs; //a copy of strings
  11. vector<int> reserved (n, 0); //a boolean indicator
  12. int left = n;
  13. for(int i = 0; i < n; i++){ //sort copy array elements in ascending orders
  14. sort(copy[i].begin(), copy[i].end());
  15. }
  16. vector<string> curr;
  17. while(left > 0){
  18. for(int i = 0; i < n; i++){
  19. if(reserved[i]==0){ //if it's not used, use the current one as a standard
  20. curr.push_back(strs[i]);
  21. reserved[i] = 1;
  22. left--;
  23. for(int j = i + 1; j < n; j++){
  24. if(reserved[j]==0 && copy[i] == copy[j]){ //compare with organized copy vector
  25. curr.push_back(strs[j]); //push all anagrams in original vector
  26. reserved[j] = 1; //show that this place is used
  27. left--;
  28. }
  29. }
  30. result.push_back(curr);
  31. curr.clear();
  32. }
  33. }
  34. }
  35. return result;
  36. }
  37. };

4. Longest Substring Without Repeating Characters

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/779/

5. Longest Palindromic Substring

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/780/

filter with sliding window and sub function:

  1. class Solution {
  2. public:
  3. string longestPalindrome(string s) {
  4. int n = s.length();
  5. if(n <= 1) //extreme cases
  6. return s;
  7. for(int len = n; len > 0; len--){ //length of the possible longest palindrome
  8. for(int i = 0; i <= n - len; i++){ //left index of that palindrome
  9. if(s[i] == s[i + len - 1]){
  10. if(isPalindrome(s.substr(i, len)))
  11. return s.substr(i, len);
  12. }
  13. }
  14. }
  15. return "";
  16. }
  17. private:
  18. bool isPalindrome(string s){ //boolean to show is this substring a palindromic substring
  19. int l = 0;
  20. int r = s.length() - 1;
  21. while(l < r){
  22. if(s[l] != s[r])
  23. return false;
  24. l++;
  25. r--;
  26. }
  27. return true;
  28. }
  29. };

6. Increasing Triplet Subsequence

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/781/

2 pointers and sub function:

  1. class Solution {
  2. public:
  3. bool increasingTriplet(vector<int>& nums) {
  4. int n = nums.size();
  5. if(n < 3) //unable to form a base case
  6. return false;
  7. for(int l = 0; l < n - 2; l++){
  8. for(int r = n - 1; r > l + 1; r--){
  9. if(nums[r] - nums[l] > 1){ //if get 2 possible pointers and they are not neighbors
  10. if(includeIncreasingTriplet(nums, l, r)){ //use sub function
  11. return true;
  12. }
  13. }
  14. }
  15. }
  16. return false;
  17. }
  18. private:
  19. bool includeIncreasingTriplet(vector<int>& nums, int l, int r) { //is there an increasing triplet
  20. int l_val = nums[l];
  21. int r_val = nums[r];
  22. for(int i = l + 1; i < r; i++){
  23. if(l_val < nums[i] && nums[i] < r_val){
  24. return true;
  25. }
  26. }
  27. return false;
  28. }
  29. };

7. Missing Ranges

https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/782/

introduce boundaries as part of the list

  1. class Solution {
  2. public:
  3. vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
  4. vector<string> result;
  5. //add boundaries into the list
  6. nums.push_back(lower-1);
  7. nums.push_back(upper+1);
  8. sort(nums.begin(), nums.end());
  9. string curr = "";
  10. for(int i = 0; i < nums.size() - 1; i++){
  11. curr = aRange(nums[i], nums[i+1]);
  12. if(curr.length() > 0) //push possible missing ranges
  13. result.push_back(curr);
  14. }
  15. return result;
  16. }
  17. private:
  18. string aRange(int a, int b){
  19. if(b-a <= 1) //if no missing values
  20. return "";
  21. else if(b-a == 2) //if one missing value
  22. return to_string(a + 1);
  23. return to_string(a + 1) + "->" + to_string(b - 1); //if over 2 missing values
  24. }
  25. };