From c1ab58230da8e607947fb4c70fe32aee75d7c25e Mon Sep 17 00:00:00 2001 From: s50600822 Date: Mon, 20 Nov 2023 20:30:21 +1100 Subject: [PATCH] haiz --- .../algorithm/java/ide_handicapped/Readme.md | 3 ++ .../longest_valid_parentheses/Solution.java | 35 +++++++++++++ .../reverse_nodes_in_k_group/Solution.java | 49 ++++++++++++++++++ .../Solution.java | 51 +++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 interview_prep/algorithm/java/ide_handicapped/Readme.md create mode 100644 interview_prep/algorithm/java/ide_handicapped/longest_valid_parentheses/Solution.java create mode 100644 interview_prep/algorithm/java/ide_handicapped/reverse_nodes_in_k_group/Solution.java create mode 100644 interview_prep/algorithm/java/ide_handicapped/substring_with_concatenation_of_all_words/Solution.java diff --git a/interview_prep/algorithm/java/ide_handicapped/Readme.md b/interview_prep/algorithm/java/ide_handicapped/Readme.md new file mode 100644 index 00000000..2ea0d1e3 --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/Readme.md @@ -0,0 +1,3 @@ +``` +javac Solution.java && java -ea Solution +``` \ No newline at end of file diff --git a/interview_prep/algorithm/java/ide_handicapped/longest_valid_parentheses/Solution.java b/interview_prep/algorithm/java/ide_handicapped/longest_valid_parentheses/Solution.java new file mode 100644 index 00000000..a343fe52 --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/longest_valid_parentheses/Solution.java @@ -0,0 +1,35 @@ +import java.util.Stack; + +class Solution { + public static int longestValidParenthese(String s){ + Stack stack = new Stack<>(); + int maxLength = 0; + stack.push(-1); + for(int i = 0; i< s.length(); i++){ + char c = s.charAt(i); + if(c=='('){ + stack.push(i); + } else { + stack.pop(); + if(stack.isEmpty()){ + stack.push(i); + } else { + maxLength = Math.max(maxLength, i - stack.peek()); + } + } + } + return maxLength; + } + + public static void main(String[] args) { + // assert longestValidParenthese("()") == 2 : String.format("actual %s", longestValidParenthese("()")); + verify(longestValidParenthese(""), 0); + verify(longestValidParenthese("("), 0); + verify(longestValidParenthese("()"), 2); + verify(longestValidParenthese(")()())"), 4); + } + + private static void verify(int actual, int expected){ + assert (actual == expected) : String.format("a %s, e %", actual, expected); + } +} \ No newline at end of file diff --git a/interview_prep/algorithm/java/ide_handicapped/reverse_nodes_in_k_group/Solution.java b/interview_prep/algorithm/java/ide_handicapped/reverse_nodes_in_k_group/Solution.java new file mode 100644 index 00000000..43e6a01f --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/reverse_nodes_in_k_group/Solution.java @@ -0,0 +1,49 @@ +class Solution { + public static ListNode reverseKGroup(ListNode head, int k) { + ListNode current = head; + int count = 0; + + while (current != null && count != k) { + current = current.next; + count++; + } + + if (count == k) { + current = reverseKGroup(current, k); + while (count-- > 0) { + ListNode tmp = head.next; + head.next = current; + current = head; + head = tmp; + } + head = current; + } + + return head; + } + + + static class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + + public static void main(String[] args) { + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + + ListNode rev = reverseKGroup(head, 2); + + assert rev.val == 2; + assert rev.next.val == 1; + assert rev.next.next.val == 4; + assert rev.next.next.next.val == 3; + assert rev.next.next.next.next.val == 5; + } +} \ No newline at end of file diff --git a/interview_prep/algorithm/java/ide_handicapped/substring_with_concatenation_of_all_words/Solution.java b/interview_prep/algorithm/java/ide_handicapped/substring_with_concatenation_of_all_words/Solution.java new file mode 100644 index 00000000..12d86ee7 --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/substring_with_concatenation_of_all_words/Solution.java @@ -0,0 +1,51 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.List; + +class Solution { + public static List findSubString(String s, String[] words){ + List result = new ArrayList<>(); + + if(null == s|| s.length() == 0 || null == words || words.length == 0){ + return result; + } + int wlen = words[0].length();// precondition: all words are of the same leng + int wcount = words.length; + int totalL = wcount*wlen; + Map wMap = new HashMap<>(); + for(String w: words){ + wMap.put(w, wMap.getOrDefault(w, 0) + 1); + } + + for(int i=0; i<= s.length() - totalL; i++){ + Map seen = new HashMap<>(); + int counter = 0; + while (counter < wcount) { + int startIdx = i + counter*wlen; + int endIdx = startIdx + wlen; + String currentW = s.substring(startIdx, endIdx); + if(!wMap.containsKey(currentW)){ + break; + } + seen.put(currentW, seen.getOrDefault(currentW, 0)+1); + if(seen.get(currentW) > wMap.get(currentW)){ + break; + } + counter++; + } + + if(counter == wcount){ result.add(i);} + + } + return result; + } + + public static void main(String[] args) { + List expected = List.of(0,9); + List actual = findSubString("barfoothefoobarman", new String[]{"foo","bar"}); + assert actual.size()==expected.size(); + assert actual.contains(0); + assert actual.contains(9); + } +} \ No newline at end of file