Posts

167. Two Sum 2 - Input Array is Sorted - Medium

 Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1], and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is exactly one solution. You may not use the same element twice. Your solution must use only constant extra space. Example: Input: numbers = [2,7,11,15], target = 9 Output: [1.2] Explanation: The sum of 2 and 7 is 0. There index1 = 1 and index2 = 2. We return [1,2] Constraints: 2 <= numbers.length <= 3 * 10**4 -1000 <= numbers[i] <= 1000 numbers is sorted in non-decreasing order. -1000 <= target <= 1000 Solution: var twoSum = function(numbers, target) {     const map = new Map();          for (let i...

392. Is Subsequence - Easy

 Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original characters without disturbing the relative positions of the remaining characters e.g "ace" is a subsequence of "abcde" while "aec" is not. Example: Input: s = "abc", t = "ahbgdc" Output: true Constraints: 0 <= s.length <= 100 0 <= t.length <= 10**4 s and t consist only of lowercase English letters Solution: var isSubsequence = function(s, t) {     let index = 0;     let string = '';     for (let i = 0; i < t.length; i++) {          if (s[index] === t[i]) {               string += t[i];               index++;          }     }       return string === s; } Thoughts: Figured out the solution on my own, which is great. It's not th...

125. Valid Palindrome - Easy

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome or false otherwise. Example: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Constraints: 1 <= s.length <= 2 * 10**5 s consists only of printable ASCII characters Solution: var isPalindorme = function(s) {     const string = s.toLowerCase().replace(/[a-z0-9]/g, '');     const reverse = string.split('').reverse().join('');     return string === reverse } Thoughts: 9/10 using all the built-in js methods.

28. Find the Index of the First Occurence in a String - Easy

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if the needle is not part of haystack. Example: Input: haystack = 'sadbutsad', needle = 'sad' Output: 0 Explanation: 'sad' occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. Constraints: 1 <= haystack.length, needle.length <= 10**4 haystack and needle consist of only lowercase English characters. Solution: var strStr = function(haystack, needle) {          return haystack.indexOf(needle); } Thoughts: 10/10 easy.

151. Reverse Words in a String - Medium

 Given an input string s, reverse the order of the words. Return a string of the words in reverse order concatenated by a single space. Example:  Input: s = "a good    example" Output: "example good a" Explanation: You need to reduce multiple spaces in the reversed string. Constraints: 1 <= s.length <= 10**4 s contains English letters There is at least one word in s. Solution: var reverseWords = function(s) {          return s.split(' ').reverse().filter(x => x !== '').join(' ');  } Thoughts: Happy with the one-line solution. I would struggle to solve this if I couldn't use the built-in js methods (reverse especially). The solution beats 49% of solutions, which is better than I thought. I did need to debug what s looked like after I split on all the spaces and then that's when I clocked so we could filter out the empty array values. I'd give my understanding an 8/10. Happy that it's a medium.

14. Longest Common Prefix - Easy

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example: Input: strs = ["flower","flow","flight"] Output: "fl" Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i] consists of only lowercase English letters if it is non-empty Solution: var longestCommonPrefix = function(strs) {     let res = '';      let prefix = strs[0];     for (let i = 0; i < prefix.length; i++) {               if (strs.some(str => str[i] !== prefix[i])) {                    break;                  }               res += prefix[i];          }     return res; } Thoughts: This was a difficult one and I gave up after 30 minutes. The s...

58. Length of Last Word - Easy

Given a string s consisting of words and spaces, return the length of the last word in the string. A word is. a maximal substring consisting of non-space characters only. Example: Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5. Constraints: 1 <= s.length <= 10**4 s consists of only English letters and spaces ' '. There will be at least one word in s. Solution: var lenghtOfLastWord = function(s) {       return s.trim().split(' ').pop().length; } Thoughts: Super simple. 10/10 for confidence and understanding.