This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const isDifferByOne = (s1, s2) => { | |
| if(s1.length > s2.length + 1 || | |
| s2.length > s1.length + 1) { | |
| return false; | |
| } | |
| let count = 0; | |
| let charMap = new Map(); | |
| let a1 = s1.split(''); | |
| let a2 = s2.split(''); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const compressString = (s1) => { | |
| let charMap = new Map(); | |
| for(let i=0; i<s1.length; i++){ | |
| charMap.set(s1[i], charMap.get(s1[i]) + 1 || 1); | |
| } | |
| let s = '', | |
| mapKeys = charMap.keys(), | |
| mapValues = charMap.values(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const rotateMatrix = (m) => { | |
| let dimensions = [m.length, m[0].length]; | |
| let target = new Array(dimensions[1]); | |
| for (let i = 0; i < dimensions[1]; i++) { | |
| target[i] = new Array(dimensions[0]); | |
| } | |
| for(let r=0; r<dimensions[0]; ++r) { //rows | |
| for(let c=0; c<dimensions[1]; ++c) { //cols | |
| target[c][dimensions[0] - r - 1] = m[r][c]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const rotate = (m) => { | |
| const n = m.length, | |
| ny = m[0].length; | |
| if(n != ny) return; //undefined if not square matrix, cannot rotate in place | |
| let temp; | |
| m = m.reverse(); //reverse rows: bottom row placed on top | |
| //perform swap | |
| for (let r = 0; r < n; r++) { | |
| for (let c = 0; c < r; c++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const zeroMatrix = (m) => { | |
| const sizeY = m.length, | |
| sizeX = m[0].length; | |
| let charMap = new Map(); | |
| for(let r=0; r<sizeY; r++){ | |
| for(let c=0; c<sizeX; c++){ | |
| if(m[r][c] === 0) { | |
| charMap.set(r, c); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const isSubstring = (s1, s2) => { | |
| if (!s1 || !s2) { | |
| return false; | |
| } | |
| if (s1.length !== s2.length) { | |
| return false; | |
| } | |
| return (s1 + s1).includes(s2); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const median = (a1, a2) => { | |
| let x = a1.concat(a2); | |
| x.sort(function (a,b) { | |
| return a - b; | |
| }); | |
| let len = x.length; | |
| return len%2 === 0 ? (x[Math.floor(len/2)-1] + x[Math.ceil(len/2)])/2 : x[Math.floor(len/2)]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const twoSum = (a, target) => { | |
| let len = a.length; | |
| if(a.length === 2) return [0, 1]; | |
| for(let i=0; i<len; i++) { | |
| for (let j = i + 1; j < len; j++) { | |
| if(a[j] === target - a[i]) { | |
| return [i,j]; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const lengthOfLongestSubstring = (s) => { | |
| if(s === '') return 0; | |
| let a = s.split(''); | |
| let newS = ''; | |
| let len = 1; | |
| let words =a[0]; | |
| let sLength = a.length; | |
| for(let i=0; i<sLength; i++){ | |
| newS = a[i]; | |
| let wordLen = sLength; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const longestPalindrome = (s) => { | |
| const reverseS = s.split('').reverse().join(''); | |
| let len = s.length; | |
| let currentWord = ''; | |
| let maxWord = s[0]; | |
| //compare original string s with reverse string | |
| for(let i=0; i<len; i++){ | |
| currentWord = ''; | |
| for(let j=i; j<len; j++){ //note index j starts at i, not 0 |