diff --git a/clone-graph/njngwn.java b/clone-graph/njngwn.java new file mode 100644 index 000000000..d64ef353f --- /dev/null +++ b/clone-graph/njngwn.java @@ -0,0 +1,40 @@ +/* +// Definition for a Node. +class Node { + public int val; + public List neighbors; + public Node() { + val = 0; + neighbors = new ArrayList(); + } + public Node(int _val) { + val = _val; + neighbors = new ArrayList(); + } + public Node(int _val, ArrayList _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ + +class Solution { + HashMap nodeMap = new HashMap<>(); // key: original Node, value: copied Node + + public Node cloneGraph(Node node) { + if (node == null) return null; + + if (nodeMap.containsKey(node)) { + return nodeMap.get(node); + } + + Node newNode = new Node(node.val); + nodeMap.put(node, newNode); + + for (Node neighborNode : node.neighbors) { + newNode.neighbors.add(cloneGraph(neighborNode)); + } + + return newNode; + } +} diff --git a/longest-repeating-character-replacement/njngwn.java b/longest-repeating-character-replacement/njngwn.java new file mode 100644 index 000000000..a323a3451 --- /dev/null +++ b/longest-repeating-character-replacement/njngwn.java @@ -0,0 +1,48 @@ +// Time Complexity: O(n), n: s.length() +// Space Complexity: O(1) +class Solution { + public int characterReplacement(String s, int k) { + int[] charFreqArr = new int[26]; // s consists of only uppercase English letters + int maxFreq = 0; + int maxLength = 0; + + for (int left = 0, right = 0; right < s.length(); right++) { + char letter = s.charAt(right); + int letterIdx = letter-'A'; + + charFreqArr[letterIdx]++; + maxFreq = Math.max(maxFreq, charFreqArr[letterIdx]); + + // when left idx moves rightward? -> k + maxFreq < size of sliding window + // here, we don't neet to recalculate maxFreq because the point is to calculate 'best' maxFreq + if (maxFreq + k < right-left+1) { + char leftChar = s.charAt(left); + charFreqArr[leftChar-'A']--; + left++; + } + + maxLength = Math.max(maxLength, right-left+1); + } + + return maxLength; + } +} + +// AABABBA, k=1, maxFreq=1, maxLength=1 +// l +// r + +// AABABBA, k=1, maxFreq=2, maxLength=2 +// lr + +// AABABBA, k=1, maxFreq=2, maxLength=3 +// l r + +// AABABBA, k=1, maxFreq=3, maxLength=4 +// l r + +// AABABBA, k=1, maxFreq=3, maxLength=4 ==> fail +// l r + +// AABABBA, k=1, maxFreq=3 +// l r diff --git a/palindromic-substrings/njngwn.java b/palindromic-substrings/njngwn.java new file mode 100644 index 000000000..92fa50dd0 --- /dev/null +++ b/palindromic-substrings/njngwn.java @@ -0,0 +1,36 @@ +// Time Complexity: O(n*n), n: s.length() +// Space Complexity: O(1) +class Solution { + public int countSubstrings(String s) { + int cnt = 0; + + for (int i = 0; i < s.length(); i++, cnt++) { // i: center of palindrom + // odd number palindrom: e.g. aba + int left = i-1, right = i+1; + + while (left >= 0 && right < s.length()) { + if (s.charAt(left) != s.charAt(right)) { + break; + } + cnt++; + left--; + right++; + } + + // even number palindrom: e.g. abba + left = i; + right = i+1; + + while (left >= 0 && right < s.length()) { + if (s.charAt(left) != s.charAt(right)) { + break; + } + cnt++; + left--; + right++; + } + } + + return cnt; + } +}