Skip to content

Commit f836481

Browse files
committed
Updated JUnit version + Repeated DNA Sequence done
1 parent b0c0959 commit f836481

File tree

6 files changed

+160
-5
lines changed

6 files changed

+160
-5
lines changed

src/main/java/com/leetcode/arrays/CanPlaceFlowers.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.leetcode.arrays;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
3+
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
/**
78
* Level: Easy

src/main/java/com/leetcode/arrays/NestedListWeightSumI.java

+8
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@
55
* @since 2019-07-27
66
*/
77
public class NestedListWeightSumI {
8+
9+
public long nestedSum(Object[] nestedList, long sum, int depth) {
10+
return -1;
11+
}
12+
13+
public static void main(String[] args) {
14+
15+
}
816
}

src/main/java/com/leetcode/dynamicprogramming/PaintHouseII.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.leetcode.dynamicprogramming;
22

3-
import static org.junit.Assert.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
/**
66
* Level: Hard
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
11
package com.leetcode.maps;
22

3+
import java.util.*;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
37
/**
48
* @author rampatra
59
* @since 2019-07-29
610
*/
711
public class RepeatedDnaSequence {
8-
}
12+
13+
/**
14+
* Rabin-Karp Algorithm: https://brilliant.org/wiki/rabin-karp-algorithm/
15+
*
16+
* @param s
17+
* @return
18+
*/
19+
public static List<String> findRepeatedDnaSequences(String s) {
20+
if (s.length() < 10) return new ArrayList<>();
21+
22+
Set<String> repeatedSequences = new HashSet<>();
23+
Map<Long, Set<String>> hashToStringMap = new HashMap<>();
24+
long hashOfSequence = computeHash(s);
25+
hashToStringMap.put(hashOfSequence, new HashSet<String>() {{
26+
add(s.substring(0, 10));
27+
}});
28+
29+
long pow = (long) Math.pow(4, 9);
30+
31+
for (int i = 10; i < s.length(); i++) {
32+
hashOfSequence = (hashOfSequence - (pow * (s.charAt(i - 10) - 'A'))) * 4 + (s.charAt(i) - 'A');
33+
String subString = s.substring(i - 10 + 1, i + 1);
34+
35+
if (hashToStringMap.get(hashOfSequence) != null && hashToStringMap.get(hashOfSequence).contains(subString)) {
36+
repeatedSequences.add(subString);
37+
continue;
38+
}
39+
40+
hashToStringMap.putIfAbsent(hashOfSequence, new HashSet<>());
41+
hashToStringMap.get(hashOfSequence).add(subString);
42+
}
43+
44+
return new ArrayList<>(repeatedSequences);
45+
}
46+
47+
private static long computeHash(String s) {
48+
long hash = 0;
49+
for (int i = 0; i < 10; i++) {
50+
hash += (Math.pow(4, i) * (s.charAt(9 - i) - 'A'));
51+
}
52+
return hash;
53+
}
54+
55+
public static void main(String[] args) {
56+
57+
assertEquals(new ArrayList<>(),
58+
findRepeatedDnaSequences("AAAAACCC"));
59+
60+
assertEquals(Arrays.asList("AAAAACCCCC", "CCCCCAAAAA"),
61+
findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"));
62+
63+
assertEquals(Collections.singletonList("AAAAAAAAAA"),
64+
findRepeatedDnaSequences("AAAAAAAAAAAA"));
65+
66+
assertEquals(Collections.singletonList("BBBBBBBBBB"),
67+
findRepeatedDnaSequences("BBBBBBBBBBBB"));
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,93 @@
11
package com.leetcode.stacks;
22

3+
import java.util.Stack;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
37
/**
8+
* Level: Medium
9+
* Problem Link: https://leetcode.com/problems/evaluate-reverse-polish-notation
10+
* Problem Description:
11+
* Evaluate the value of an arithmetic expression in Reverse Polish Notation.
12+
* <p>
13+
* Valid operators are +, -, *, /. Each operand may be an integer or another expression.
14+
* <p>
15+
* Note:
16+
* Division between two integers should truncate toward zero.
17+
* The given RPN expression is always valid. That means the expression would always evaluate to a result and there
18+
* won't be any divide by zero operation.
19+
* <p>
20+
* Example 1:
21+
* Input: ["2", "1", "+", "3", "*"]
22+
* Output: 9
23+
* Explanation: ((2 + 1) * 3) = 9
24+
* <p>
25+
* Example 2:
26+
* Input: ["4", "13", "5", "/", "+"]
27+
* Output: 6
28+
* Explanation: (4 + (13 / 5)) = 6
29+
* <p>
30+
* Example 3:
31+
* Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
32+
* Output: 22
33+
* Explanation:
34+
* ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
35+
* = ((10 * (6 / (12 * -11))) + 17) + 5
36+
* = ((10 * (6 / -132)) + 17) + 5
37+
* = ((10 * 0) + 17) + 5
38+
* = (0 + 17) + 5
39+
* = 17 + 5
40+
* = 22
41+
*
442
* @author rampatra
543
* @since 2019-07-27
644
*/
745
public class ReversePolishNotation {
46+
47+
/**
48+
* Time Complexity:
49+
* Space Complexity:
50+
* Runtime: <a href="https://leetcode.com/submissions/detail/246794713/">5 ms</a>.
51+
*
52+
* @param tokens
53+
* @return
54+
*/
55+
public static int evalRPN(String[] tokens) {
56+
int operand1;
57+
int operand2;
58+
59+
Stack<Integer> stack = new Stack<>();
60+
61+
for (String s : tokens) {
62+
switch (s) {
63+
case "+":
64+
stack.push(stack.pop() + stack.pop());
65+
break;
66+
case "-":
67+
operand1 = stack.pop();
68+
operand2 = stack.pop();
69+
stack.push(operand2 - operand1);
70+
break;
71+
case "*":
72+
stack.push(stack.pop() * stack.pop());
73+
break;
74+
case "/":
75+
operand1 = stack.pop();
76+
operand2 = stack.pop();
77+
stack.push(operand2 / operand1);
78+
break;
79+
default:
80+
stack.push(Integer.parseInt(s));
81+
}
82+
}
83+
84+
return stack.pop();
85+
}
86+
87+
public static void main(String[] args) {
88+
assertEquals(18, evalRPN(new String[]{"18"}));
89+
assertEquals(9, evalRPN(new String[]{"2", "1", "+", "3", "*"}));
90+
assertEquals(6, evalRPN(new String[]{"4", "13", "5", "/", "+"}));
91+
assertEquals(22, evalRPN(new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}));
92+
}
893
}

src/main/java/com/leetcode/trees/SymmetricTree.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedList;
44
import java.util.Queue;
55

6-
import static org.junit.Assert.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
77

88
/**
99
* Level: Easy

0 commit comments

Comments
 (0)