Skip to content

[hsskey] Week 15 Solutions #1666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2025
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
55 changes: 55 additions & 0 deletions alien-dictionary/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export class Solution {
/**
* @param {string[]} words
* @return {string}
*/
alienOrder(words) {
const adj = new Map();
for (const word of words) {
for (const char of word) {
if (!adj.has(char)) {
adj.set(char, new Set());
}
}
}

for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i];
const w2 = words[i + 1];
const minLen = Math.min(w1.length, w2.length);

if (w1.length > w2.length && w1.slice(0, minLen) === w2.slice(0, minLen)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice 대신 startsWith 를 써도 괜찮을 것 같아요!

if (w1.length > w2.length && w1.startsWith(w2)) return "";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice만 주로 많이써서 이용해봤는데 리뷰 감사합니다!

return "";
}

for (let j = 0; j < minLen; j++) {
if (w1[j] !== w2[j]) {
adj.get(w1[j]).add(w2[j]);
break;
}
}
}

const visit = {}; // false: visited, true: current path (cycle detection)
const res = [];

const dfs = (c) => {
if (c in visit) return visit[c];

visit[c] = true;
for (const nei of adj.get(c)) {
if (dfs(nei)) return true;
}
visit[c] = false;
res.push(c);
return false;
};

for (const c of adj.keys()) {
if (dfs(c)) return "";
}

return res.reverse().join('');
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/

/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
if (!preorder.length || !inorder.length) {
return null;
}

const root = new TreeNode(preorder[0]);
const mid = inorder.indexOf(preorder[0]);

root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));

return root;
};

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

for (let i = 0; i < s.length; i++) {
// Odd length
let l = i,
r = i;
while (l >= 0 && r < s.length && s[l] === s[r]) {
if (r - l + 1 > resLen) {
res = s.slice(l, r + 1);
resLen = r - l + 1;
}
l--;
r++;
}

// Even length
l = i, r = i + 1;
while (l >= 0 && r < s.length && s[l] === s[r]) {
if (r - l + 1 > resLen) {
res = s.slice(l, r + 1);
resLen = r - l + 1;
}
l--;
r++;
}
}

return res;
};

33 changes: 33 additions & 0 deletions rotate-image/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
let l = 0;
let r = matrix.length - 1;

while (l < r) {
for (let i = 0; i < r - l; i++) {
let top = l;
let bottom = r;

// save the top-left
let topLeft = matrix[top][l + i];

// move bottom-left into top-left
matrix[top][l + i] = matrix[bottom - i][l];

// move bottom-right into bottom-left
matrix[bottom - i][l] = matrix[bottom][r - i];

// move top-right into bottom-right
matrix[bottom][r - i] = matrix[top + i][r];

// move top-left into top-right
matrix[top + i][r] = topLeft;
}
r--;
l++;
}
};

39 changes: 39 additions & 0 deletions subtree-of-another-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/

/**
* @param {TreeNode} s
* @param {TreeNode} t
* @return {boolean}
*/
var isSubtree = function(s, t) {
if (!t) return true;
if (!s) return false;

if (sameTree(s, t)) {
return true;
}
return isSubtree(s.left, t) || isSubtree(s.right, t);
};

/**
* @param {TreeNode} s
* @param {TreeNode} t
* @return {boolean}
*/
var sameTree = function(s, t) {
if (!s && !t) return true;

if (s && t && s.val === t.val) {
return sameTree(s.left, t.left) && sameTree(s.right, t.right);
}

return false;
};