Two Pointers Flashcards
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: “A man, a plan, a canal: Panama”
Output: true
/** * @param {string} s * @return {boolean} */ var isPalindrome = function(s) { const reg = new RegExp('^[a-z0-9]$'); let sIndex = 0; let eIndex = s.length - 1; const alphanumericString = s.match(reg); while (sIndex < eIndex) { let letterA = s[sIndex].toLowerCase(); let letterB = s[eIndex].toLowerCase(); if (reg.test(letterA) && reg.test(letterB)) { if (letterA !== letterB) { return false; } sIndex++; eIndex--; } else { if (!reg.test(letterA)) { sIndex++; } else { eIndex--; } }
} return true; };
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: “hello”
Output: “holle”
/** * @param {string} s * @return {string} */ var reverseVowels = function(s) { const vowels = ['a', 'e', 'i', 'o', 'u'];
let j = 0; // Storing the vowels separately const str = s.split(''); let vowel = ""; for (let i = 0; i < str.length; i++) { if (vowels.includes(str[i].toLowerCase())) { j++; vowel += str[i]; } }
// Placing the vowels in the reverse // order in the string for (let i = 0; i < str.length; i++) { if (vowels.includes(str[i].toLowerCase())) { str[i] = vowel[--j]; } }
return str.join(''); };
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
/** * @param {number[]} numbers * @param {number} target * @return {number[]} */ var twoSum = function(numbers, target) { let low = 0; let high = numbers.length - 1;
while (low < high) { const sum = numbers[high] + numbers[low]; if (sum === target) { return [low + 1, high + 1]; }
if (sum > target) { high -= 1; } else { low += 1; } }
return []; };
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
/** * @param {number[]} nums * @param {number} val * @return {number} */ var removeElement = function(nums, val) { if (nums === null || nums.length == 0) { return 0; }
let pointer = 0; for (let i = 0; i < nums.length; i++) { if (nums[i] !== val) { nums[pointer] = nums[i]; pointer++; } } };
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var moveZeroes = function(nums) { let pointer = 0; let zeroCounters = 0; for (let i = 0; i < nums.length; i++) { if (nums[i] !== 0) { nums[pointer] = nums[i]; pointer++; } else { zeroCounters++ } } let i = 1; while (zeroCounters) { nums[nums.length - i] = 0; zeroCounters--; i++; }
};
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var moveZeroes = function(nums) { let pointer = 0; for (let i = 0; i < nums.length; i++) { if (nums[i] !== 0) { nums[pointer] = nums[i]; pointer++; } } while (pointer < nums.length) { nums[pointer] = 0; pointer++; }
};
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { const result = [] nums1.sort((a, b) => a - b); nums2.sort((a, b) => a - b);
let pointer = 0; for (let i = 0; i < nums1.length; i++) { while (pointer + 1 < nums2.length && nums2[pointer] < nums1[i]) pointer++; if (pointer < nums2.length && nums1[i] == nums2[pointer]) { result.push(nums1[i]); pointer++; } }
return result; };
You are given a sorted unique integer array nums.
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
Each range [a,b] in the list should be output as:
“a->b” if a != b
“a” if a == b
Example 1:
Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7" Example 2:
Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
/** * @param {number[]} nums * @return {string[]} */ var summaryRanges = function(nums) { const result = [];
let l = 0, r = 0; while (l < nums.length) { r = l; while (r + 1 < nums.length && nums[r] + 1 == nums[r + 1]) r++;
if (l == r) result.push(String(nums[l])); else result.push(nums[l] + "->" + nums[r]); l = r + 1; } return result; };
- Remove Duplicates from Sorted Array
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2]
Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the returned length.
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn’t matter what values are set beyond the returned length.
/** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { if (nums.length == 0) return 0; let i = 0; for (let j = 1; j < nums.length; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; };
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) { const hashTable = {}; for (let i = 0; i < nums.length; i++) { hashTable[nums[i]] = i; }
for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; if (hashTable[complement] && hashTable[complement] !== i) { return [i, hashTable[complement]]; } } };