Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions clone-graph/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* // Definition for a Node.
* function _Node(val, neighbors) {
* this.val = val === undefined ? 0 : val;
* this.neighbors = neighbors === undefined ? [] : neighbors;
* };
*/

/**
* @param {_Node} node
* @return {_Node}
*/
var cloneGraph = function(node) {
if (!node) return null;

const oldToNew = new Map();

const dfs = (node) => {
if (oldToNew.has(node)) {
return oldToNew.get(node);
}

const copy = new _Node(node.val);
oldToNew.set(node, copy);

for (let neighbor of node.neighbors) {
copy.neighbors.push(dfs(neighbor));
}

return copy;
};

return dfs(node);
};
23 changes: 23 additions & 0 deletions longest-common-subsequence/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function(text1, text2) {
const m = text1.length;
const n = text2.length;

const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
if (text1[i] === text2[j]) {
dp[i][j] = 1 + dp[i + 1][j + 1];
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
}

return dp[0][0];
};
25 changes: 25 additions & 0 deletions longest-repeating-character-replacement/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var characterReplacement = function(s, k) {
const count = {};
let res = 0;
let l = 0;
let maxf = 0;

for (let r = 0; r < s.length; r++) {
count[s[r]] = (count[s[r]] || 0) + 1;
maxf = Math.max(maxf, count[s[r]]);

while ((r - l + 1) - maxf > k) {
count[s[l]] -= 1;
l += 1;
}

res = Math.max(res, r - l + 1);
}

return res;
};
24 changes: 24 additions & 0 deletions palindromic-substrings/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function(s) {
let res = 0;

const countPali = (s, l, r) => {
let count = 0;
while (l >= 0 && r < s.length && s[l] === s[r]) {
count++;
l--;
r++;
}
return count;
};

for (let i = 0; i < s.length; i++) {
res += countPali(s, i, i);
res += countPali(s, i, i + 1);
}

return res;
};
12 changes: 12 additions & 0 deletions reverse-bits/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function(n) {
let res = 0;
for (let i = 0; i < 32; i++) {
let bit = (n >>> i) & 1;
res |= (bit << (31 - i));
}
return res >>> 0;
};