# Leetcode Notes ###### tags: `Leetcode` ## 1768. Merge Strings Alternately ### 2024.01.10 C++ Version 1: ```cpp= class Solution { public: string mergeAlternately(string word1, string word2) { int len_word1 = word1.length(); int len_word2 = word2.length(); string concate = ""; if (len_word1 > len_word2){ for(int i=0; i<len_word2; i++){ concate = concate + word1[i] + word2[i]; } for(int j=len_word2; j<len_word1; j++){ concate = concate + word1[j]; } } else if (len_word1 == len_word2){ for(int i=0; i<len_word2; i++){ concate = concate + word1[i] + word2[i]; } } else { for(int i=0; i<len_word1; i++){ concate = concate + word1[i] + word2[i]; } for(int j=len_word1; j<len_word2; j++){ concate = concate + word2[j]; } } return concate; } }; ``` Results: | Type | Runtime | Memory | | ------ | ------- | ------ | | Result | 3ms | 8.78MB | | Beats | 49.62% | 5.67% | ### 2024.01.10 C++ Version 2: ```cpp= class Solution { public: string mergeAlternately(string word1, string word2) { int i = 0, j = 0; int len_word1 = word1.length(); int len_word2 = word2.length(); string concate = ""; while(i < len_word1 && j < len_word2){ concate += word1[i++]; concate += word2[j++]; } while(i < len_word1){ concate += word1[i++]; } while(j < len_word2){ concate += word2[j++]; } return concate; } }; ``` Results: | Type | Runtime | Memory | | ------ | ------- | ------ | | Result | 3ms | 6.64MB | | Beats | 49.62% | 77.36% | ### 2024.01.10 Python Version 1: ```python= class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: concate = "" i = 0 j = 0 len1 = len(word1) len2 = len(word2) while i < len1 and j < len2: concate += word1[i] concate += word2[j] i += 1 j += 1 while i < len1: concate += word1[i] i += 1 while j < len2: concate += word2[j] j += 1 return concate ``` Results: | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 39ms | 17.26MB | | Beats | 52.52% | 26.97% | --- ## 1071. Greatest Common Divisor of Strings ### 2024.01.11 Python Version 1: ```python= class Solution: def gcdOfStrings(self, str1: str, str2: str) -> str: if str1 + str2 != str2 + str1: return "" len_str1 = len(str1) len_str2 = len(str2) import math return str1[0:math.gcd(len_str1, len_str2)] ``` Results: | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 37ms | 17.37MB | | Beats | 73.96% | 19.28% | ### 2024.01.11 C++ Version 1: ```cpp= class Solution { public: string gcdOfStrings(string str1, string str2) { if(str1 + str2 != str2 + str1){ return ""; } return str1.substr(0, gcd(str1.length(), str2.length())); } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 2ms | 7.53MB | | Beats | 69.15% | 58.06% | --- ## 1431. Kids With the Greatest Number of Candies ### 2024.01.13 C++ Version 1: ```cpp= class Solution { public: vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) { vector<int> after_recieve(candies.size()); vector<bool> bool_vector(candies.size()); for(int i=0;i<candies.size();i++){ after_recieve[i] = (candies[i] + extraCandies); } int largest_element = candies[0]; for(int i=0; i<candies.size(); i++){ if(candies[i] > largest_element){ largest_element = candies[i]; } } for(int i=0;i<after_recieve.size();i++){ if(after_recieve[i] >= largest_element){ bool_vector[i]= true; }else{ bool_vector[i] = false; } } return bool_vector; } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 5ms | 9.28MB | | Beats | 31.80% | 63.86% | ### 2024.01.13 C++ Version 2: ```cpp= class Solution { public: vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) { vector<bool> bool_vector(candies.size()); int largest_element = candies[0]; int size = candies.size(); for(int i=0; i<size; i++){ if(candies[i] > largest_element){ largest_element = candies[i]; } } for(int i=0; i<size; i++){ if(candies[i] + extraCandies >= largest_element){ bool_vector[i]= true; }else{ bool_vector[i] = false; } } return bool_vector; } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 0ms | 9.17MB | | Beats | 100.00% | 96.69% | --- ## 605. Can Place Flowers ### 2024.01.14 C++ Version 1: ```cpp= class Solution { public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int cnt = 0; int size = flowerbed.size(); if(size == 1){ if(flowerbed[0] == 0){ cnt += 1; } }else{ for(int i=0;i<size;i++){ if(i == 0){ if(flowerbed[i+1] == 0){ if(flowerbed[i] != 1){ flowerbed[i] = 1; cnt += 1; } } }else if (i == size-1){ if(flowerbed[i-1] == 0){ if(flowerbed[i] != 1){ cnt += 1; } } }else{ if(flowerbed[i-1] == 0 && flowerbed[i+1] == 0){ if(flowerbed[i] != 1){ flowerbed[i] = 1; cnt += 1; } } } } } if(cnt >= n){ return true; }else{ return false; } } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 7ms | 20.78MB | | Beats | 96.64% | 33.96% | ### 2024.01.14 C++ Version 2: ```cpp= class Solution { public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int cnt = 0; int size = flowerbed.size(); if(size == 1){ if(flowerbed[0] == 0){ cnt += 1; } }else{ for(int i=0;i<size;i++){ if(i == 0){ if(flowerbed[i+1] == 0 && flowerbed[i] != 1){ flowerbed[i] = 1; cnt += 1; } }else if (i == size-1){ if(flowerbed[i-1] == 0 && flowerbed[i] != 1){ cnt += 1; } }else{ if(flowerbed[i-1] == 0 && flowerbed[i] != 1 && flowerbed[i+1] == 0){ flowerbed[i] = 1; cnt += 1; } } } } if(cnt >= n){ return true; }else{ return false; } } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 11ms | 20.66MB | | Beats | 80.40% | 66.07% | --- ## 345. Reverse Vowels of a String ### 2024.01.15 C++ Version 1: ```cpp= class Solution { public: string reverseVowels(string s) { string save_string = ""; for(int i=0; i<s.length(); i++){ if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'){ save_string += s[i]; s[i] = '_'; } } int j = save_string.length() - 1; if(save_string.length()>0){ for(int i=0; i<s.length(); i++){ if(s[i]=='_'){ s[i] = save_string[j]; j -= 1; } } } return s; } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 8ms | 8.19MB | | Beats | 50.20% | 46.54% | --- ## 151. Reverse Words in a String ### 2024.01.17 C++ Version 1: ```cpp= class Solution { public: string reverseWords(string s) { vector<string> wordlist(0); string concate_word = ""; int start = 0; int end = 0; int i = 0; int j = 0; while(i < s.length()){ if(s[i]!=' '){ start = i; for(j=i; j<s.length(); j++){ if(s[j]==' '){ end = j; break; } } i = j - 1; wordlist.push_back(s.substr(start, end - start)); } i++; } if(start == end){ return s; } for(int k = 0; k < wordlist.size(); k++){ if(k == 0){ concate_word = wordlist[wordlist.size()-1-k]; }else{ concate_word = concate_word + " " + wordlist[wordlist.size()-1-k]; } } return concate_word; } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 14ms | 47.78MB | | Beats | 12.61% | 5.29% | --- ## 238. Product of Array Except Self ### 2024.01.22 C++ Version 1: ```cpp= class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> leftProd(nums.size(), 1); vector<int> rightProd(nums.size(), 1); vector<int> result(nums.size(), 1); for(int i=1; i<nums.size(); i++){ leftProd[i] = leftProd[i-1] * nums[i-1]; } for(int i=nums.size()-2; i>=0; i--){ rightProd[i] = rightProd[i+1] * nums[i+1]; } for(int i=0; i<nums.size(); i++){ result[i] = leftProd[i] * rightProd[i]; } return result; } }; ``` | Type | Runtime | Memory | | ------ | ------- | ------- | | Result | 24ms | 27.52MB | | Beats | 23.63% | 5.21% |