Skip to content

Commit

Permalink
haiz
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Nov 20, 2023
1 parent 01e57c9 commit c1ab582
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions interview_prep/algorithm/java/ide_handicapped/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```
javac Solution.java && java -ea Solution
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.Stack;

class Solution {
public static int longestValidParenthese(String s){
Stack<Integer> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> findSubString(String s, String[] words){
List<Integer> 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<String, Integer> 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<String, Integer> 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<Integer> expected = List.of(0,9);
List<Integer> actual = findSubString("barfoothefoobarman", new String[]{"foo","bar"});
assert actual.size()==expected.size();
assert actual.contains(0);
assert actual.contains(9);
}
}

0 comments on commit c1ab582

Please sign in to comment.