-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
``` | ||
javac Solution.java && java -ea Solution | ||
``` |
35 changes: 35 additions & 0 deletions
35
interview_prep/algorithm/java/ide_handicapped/longest_valid_parentheses/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
interview_prep/algorithm/java/ide_handicapped/reverse_nodes_in_k_group/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ep/algorithm/java/ide_handicapped/substring_with_concatenation_of_all_words/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |