Skip to content

Commit 0089007

Browse files
committed
Added 2 solutions & modified 1 solution
1 parent c13ae65 commit 0089007

File tree

3 files changed

+118
-12
lines changed

3 files changed

+118
-12
lines changed

Easy/N-th Tribonacci Number.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
Integer[] memo;
3+
public int tribonacci(int n) {
4+
memo = new Integer[n + 1];
5+
return helper(n);
6+
}
7+
8+
private int helper(int n) {
9+
if (n == 0) {
10+
return 0;
11+
}
12+
13+
if (n == 1 || n == 2) {
14+
return 1;
15+
}
16+
17+
if (memo[n] != null) {
18+
return memo[n];
19+
}
20+
21+
memo[n] = helper(n - 3) + helper(n - 2) + helper(n - 1);
22+
return memo[n];
23+
}
24+
}

Easy/Valid Parantheses.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
class Solution {
2+
public Map<Character, Character> map = new HashMap<Character, Character>(){{
3+
put('{', '}');
4+
put('[', ']');
5+
put('(', ')');
6+
}};
7+
28
public boolean isValid(String s) {
39
Stack<Character> stack = new Stack<>();
4-
String open = "{([";
5-
String close = "})]";
6-
for (int i=0;i<s.length();i++) {
7-
if (open.indexOf(s.charAt(i)) != -1) {
8-
stack.push(s.charAt(i));
10+
for (char c : s.toCharArray()) {
11+
if (map.containsKey(c)) {
12+
stack.push(c);
913
}
1014
else {
11-
if (!stack.empty()) {
12-
char c = stack.pop();
13-
if (open.indexOf(c) != close.indexOf(s.charAt(i))) {
14-
return false;
15-
}
15+
if (stack.isEmpty()) {
16+
return false;
1617
}
17-
else {
18+
19+
char removed = stack.pop();
20+
if (map.get(removed) != c) {
1821
return false;
1922
}
2023
}
2124
}
22-
return stack.empty();
25+
26+
return stack.isEmpty();
2327
}
2428
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class WordDictionary {
2+
3+
/** Initialize your data structure here. */
4+
TrieNode root;
5+
public WordDictionary() {
6+
root = new TrieNode();
7+
}
8+
9+
/** Adds a word into the data structure. */
10+
public void addWord(String word) {
11+
addToTrie(word);
12+
}
13+
14+
private void addToTrie(String word) {
15+
TrieNode curr = root;
16+
for (int i = 0; i < word.length(); i++) {
17+
char c = word.charAt(i);
18+
if (curr.children[c - 'a'] == null) {
19+
curr.children[c - 'a'] = new TrieNode();
20+
}
21+
22+
curr = curr.children[c - 'a'];
23+
24+
if (i == word.length() - 1) {
25+
curr.isWord = true;
26+
}
27+
}
28+
}
29+
30+
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
31+
public boolean search(String word) {
32+
TrieNode curr = root;
33+
boolean[] ans = {false};
34+
helper(word, 0, curr, ans);
35+
return ans[0];
36+
}
37+
38+
private void helper(String word, int idx, TrieNode curr, boolean[] ans) {
39+
if (curr == null) {
40+
return;
41+
}
42+
43+
if (idx == word.length()) {
44+
if (curr.isWord) {
45+
ans[0] = true;
46+
}
47+
return;
48+
}
49+
50+
if (word.charAt(idx) != '.') {
51+
helper(word, idx + 1, curr.children[word.charAt(idx) - 'a'], ans);
52+
}
53+
else {
54+
for (int i = 0; i < 26; i++) {
55+
if (curr.children[i] != null) {
56+
helper(word, idx + 1, curr.children[i], ans);
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
class TrieNode {
64+
TrieNode[] children;
65+
boolean isWord;
66+
67+
public TrieNode() {
68+
children = new TrieNode[26];
69+
isWord = false;
70+
}
71+
}
72+
73+
/**
74+
* Your WordDictionary object will be instantiated and called as such:
75+
* WordDictionary obj = new WordDictionary();
76+
* obj.addWord(word);
77+
* boolean param_2 = obj.search(word);
78+
*/

0 commit comments

Comments
 (0)