From a9e8c5f37e6ab6109aa1dbca48aa2789d4258b3e Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Fri, 2 Jan 2026 21:24:20 +0900 Subject: [PATCH] Solutions #234 & #244 & #259 & #267 & #274 --- clone-graph/ys-han00.cpp | 52 +++++++++++++++++++ longest-common-subsequence/ys-han00.cpp | 18 +++++++ .../ys-han00.cpp | 22 ++++++++ palindromic-substrings/ys-han00.cpp | 24 +++++++++ reverse-bits/ys-han00.cpp | 36 +++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 clone-graph/ys-han00.cpp create mode 100644 longest-common-subsequence/ys-han00.cpp create mode 100644 longest-repeating-character-replacement/ys-han00.cpp create mode 100644 palindromic-substrings/ys-han00.cpp create mode 100644 reverse-bits/ys-han00.cpp diff --git a/clone-graph/ys-han00.cpp b/clone-graph/ys-han00.cpp new file mode 100644 index 0000000000..d6065fd1dd --- /dev/null +++ b/clone-graph/ys-han00.cpp @@ -0,0 +1,52 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } +}; +*/ + +class Solution { +public: + Node* cloneGraph(Node* node) { + if(!node) + return nullptr; + + + Node* clone = new Node(node->val); + map clones; + queue que; + + clones[node] = clone; + que.push(node); + + while(!que.empty()) { + node = que.front(); + que.pop(); + + for(auto neigh : node->neighbors) { + if(clones.find(neigh) == clones.end()) { + que.push(neigh); + clones[neigh] = new Node(neigh->val); + } + clones[node]->neighbors.push_back(clones[neigh]); + } + } + + return clone; + } +}; + diff --git a/longest-common-subsequence/ys-han00.cpp b/longest-common-subsequence/ys-han00.cpp new file mode 100644 index 0000000000..45a9ea49fd --- /dev/null +++ b/longest-common-subsequence/ys-han00.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int longestCommonSubsequence(string text1, string text2) { + int n = text1.size(), m = text2.size(); + vector> dp(n, vector (m, 0)); + + for(int i = 0; i < n ; i++) { + for(int j = 0; j < m; j++) { + dp[i][j] = max((i == 0 ? 0 : dp[i - 1][j]), (j == 0 ? 0 : dp[i][j - 1])); + if(text1[i] == text2[j]) + dp[i][j] = (i == 0 || j == 0 ? 0 : dp[i - 1][j - 1]) + 1; + } + } + + return dp[n - 1][m - 1]; + } +}; + diff --git a/longest-repeating-character-replacement/ys-han00.cpp b/longest-repeating-character-replacement/ys-han00.cpp new file mode 100644 index 0000000000..e28ccb7fb1 --- /dev/null +++ b/longest-repeating-character-replacement/ys-han00.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int characterReplacement(string s, int k) { + int left = 0, right = 0, ans = 0; + int maxi_cnt = 0; + map cnt; + + while(left < s.size() && right < s.size()) { + cnt[s[right]]++; + maxi_cnt = max(maxi_cnt, cnt[s[right]]); + right++; + + if(right - left - maxi_cnt <= k) + ans = max(ans, right - left); + else + cnt[s[left++]]--; + } + + return ans; + } +}; + diff --git a/palindromic-substrings/ys-han00.cpp b/palindromic-substrings/ys-han00.cpp new file mode 100644 index 0000000000..1b00c63d0b --- /dev/null +++ b/palindromic-substrings/ys-han00.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int countSubstrings(string s) { + int ans = 0, n = s.size(); + for(int i = 0; i < n; i++) { + int left = i, right = i; + while(left >= 0 && right < n && s[left] == s[right]) { + ans++; + left--; + right++; + } + + left = i, right = i + 1; + while(left >= 0 && right < n && s[left] == s[right]) { + ans++; + left--; + right++; + } + } + + return ans; + } +}; + diff --git a/reverse-bits/ys-han00.cpp b/reverse-bits/ys-han00.cpp new file mode 100644 index 0000000000..c6721e4d99 --- /dev/null +++ b/reverse-bits/ys-han00.cpp @@ -0,0 +1,36 @@ +// class Solution { +// public: +// int reverseBits(int n) { +// string bits = ""; + +// while(n) { +// bits += (n % 2) + '0'; +// n /= 2; +// } + +// while(bits.size() < 32) +// bits += '0'; + +// int ans = 0, d = 1; +// for(int i = 31; i >= 0; i--) { +// ans += d * (bits[i] - '0'); +// d <<= 1; +// } + +// return ans; +// } +// }; + +class Solution { +public: + int reverseBits(int n) { + int ans = 0; + for(int i = 0; i < 32; i++) { + ans <<= 1; + ans |= (n & 1); + n >>= 1; + } + return ans; + } +}; +