Skip to content

Commit 7ebf9d7

Browse files
Merge branch 'master' into master
2 parents 47c7414 + af0b0bb commit 7ebf9d7

64 files changed

Lines changed: 1149 additions & 317 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Backtracking/Algorithms_ToDo.md‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Algorithms ToDo
22

3-
+ Valid Sudoku [LeetCode #36](https://leetcode.com/problems/valid-sudoku/)
4-
+ Regular Expression Matching [LeetCode #10](https://leetcode.com/problems/regular-expression-matching/)
5-
+ Letter Combination of a Phone Number [LeetCode #17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
6-
+ Generate Parentheses [LeetCode #22](https://leetcode.com/problems/generate-parentheses/)
7-
+ Combination Sum [LeetCode #39](https://leetcode.com/problems/combination-sum/)
8-
+ Wildcard Matching [LeetCode #44](https://leetcode.com/problems/wildcard-matching/)
3+
+ Wildcard Matching [LeetCode #44](https://leetcode.com/problems/wildcard-matching/)
4+
+ Permutations LeetCode #46
5+
+ Permutations II LeetCode #47
6+
+ Combinations LeetCode #77
7+
+ Subsets LeetCode #78

‎Backtracking/CombinationSum.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function(candidates, target) {
7+
var result = [];
8+
9+
if (candidates == null || candidates.length === 0) {
10+
return result;
11+
};
12+
13+
var current = [];
14+
candidates.sort();
15+
16+
combinationSumHelper(candidates, target, 0, current, result);
17+
18+
return result;
19+
};
20+
21+
function combinationSumHelper(candidates, target, j, curr, result){
22+
if(target === 0){
23+
var temp = curr.slice();
24+
result.push(temp);
25+
return;
26+
}
27+
28+
for(var i=j; i<candidates.length; i++){
29+
if(target < candidates[i]) {
30+
return;
31+
}
32+
curr.push(candidates[i]);
33+
combinationSumHelper(candidates, target - candidates[i], i, curr, result);
34+
curr.pop();
35+
}
36+
}
37+
38+
const candidates = [2,3,6,7];
39+
const target = 7;
40+
console.log(combinationSum(candidates, target));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {string} digits
3+
* @return {string[]}
4+
*/
5+
var letterCombinations = function(digits) {
6+
let sanitizeInput = function(unsanitizedDigits) {
7+
let regex = /(\W|_|[0-1a-zA-Z])/g;
8+
let sanitized = unsanitizedDigits.replace(regex, "");
9+
return sanitized;
10+
};
11+
const mapping = {
12+
2: ["a", "b", "c"],
13+
3: ["d", "e", "f"],
14+
4: ["g", "h", "i"],
15+
5: ["j", "k", "l"],
16+
6: ["m", "k", "o"],
17+
7: ["p", "q", "r", "s"],
18+
8: ["t", "u", "v"],
19+
9: ["w", "x", "y", "z"],
20+
};
21+
let sanitizedDigits = sanitizeInput(digits);
22+
if (sanitizedDigits.length > 12) {
23+
return "1,388,674 combinations should be more than enough for you. Provide 12 or fewer digits.";
24+
}
25+
if (sanitizedDigits.length < 1) {
26+
return "Nothing to return.";
27+
}
28+
let finalArray = [];
29+
30+
const backtrackRecursive = function(combination, nextDigits) {
31+
if (nextDigits.length === 0) {
32+
finalArray.push(combination);
33+
} else {
34+
for (let i = 0; i < mapping[nextDigits[0]].length; i += 1) {
35+
backtrackRecursive(
36+
combination + mapping[nextDigits[0]][i],
37+
nextDigits.slice(1)
38+
);
39+
}
40+
}
41+
};
42+
43+
if (sanitizedDigits) {
44+
backtrackRecursive("", sanitizedDigits);
45+
}
46+
47+
return finalArray;
48+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
const generateParenthesis = function(n) {
6+
if (n === 0) {
7+
return [""];
8+
}
9+
let result = [];
10+
for (let i = 0; i < n; ++i) {
11+
let lefts = generateParenthesis(i);
12+
let rights = generateParenthesis(n - i - 1);
13+
14+
for (let l = 0; l < lefts.length; ++l) {
15+
for (let r = 0; r < rights.length; ++r) {
16+
result.push("(" + lefts[l] + ")" + rights[r]);
17+
}
18+
}
19+
}
20+
return result;
21+
};
22+
23+
console.log(generateParenthesis(3));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
7+
var isMatch = function(s, p) {
8+
if (p === "") return s === "";
9+
var first_match = (s!="" && (p[0] == s[0] || p[0] == '.'));
10+
11+
if (p.length >= 2 && p[1] == '*'){
12+
return (isMatch(s, p.substring(2)) ||
13+
(first_match && isMatch(s.substring(1), p)));
14+
} else {
15+
return first_match && isMatch(s.substring(1), p.substring(1));
16+
}
17+
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Algorithms ToDo
22

33
+ avl tree
4-
+ basic binary tree
5-
+ binary search tree
4+
+ basic binary tree

‎DataStructures/Linked List/Algorithms_toDo.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Algorithms ToDo
22

3-
+ linked list
43
+ double linked list
54
+ is palindrome
65
+ sqap nodes
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const node = require('./Node');
2+
let Node = node.Node;
3+
4+
class LinkedList {
5+
constructor() {
6+
this.head = null;
7+
}
8+
9+
append(data) {
10+
if (this.head === null) {
11+
this.head = new Node(data);
12+
return;
13+
}
14+
15+
let currentNode = this.head;
16+
17+
while (currentNode.next !== null) {
18+
currentNode = currentNode.next;
19+
}
20+
21+
currentNode.next = new Node(data);
22+
}
23+
24+
prepend(data) {
25+
const newHead = new Node(data);
26+
newHead.next = this.head;
27+
this.head = newHead;
28+
}
29+
30+
deleteWithValue(key, value) {
31+
if (this.head === null) return false;
32+
33+
if (this.head.data[key] === value) {
34+
this.head = this.head.next;
35+
return true;
36+
}
37+
38+
let currentNode = this.head;
39+
40+
while (currentNode.next !== null) {
41+
if (currentNode.next.data[key] === value) {
42+
currentNode.next = currentNode.next.next;
43+
return true;
44+
}
45+
currentNode = currentNode.next;
46+
}
47+
48+
return false;
49+
}
50+
51+
getNodeWithValue(key, value) {
52+
if (this.head === null) return null;
53+
54+
let currentNode = this.head;
55+
56+
if (this.head.data[key] === value) {
57+
return this.head;
58+
}
59+
60+
while (currentNode !== null) {
61+
if (currentNode.data[key] === value) {
62+
return currentNode;
63+
}
64+
65+
currentNode = currentNode.next;
66+
}
67+
68+
return null;
69+
}
70+
}
71+
72+
module.exports = {LinkedList: LinkedList}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const linked = require('./LinkedList');
2+
const node = require('./Node');
3+
let LinkedList = linked.LinkedList;
4+
let Node = node.Node;
5+
6+
test('we can create a new linked list', () => {
7+
const ll = new LinkedList();
8+
expect(ll).toBeDefined();
9+
expect(ll).toHaveProperty('head');
10+
});
11+
12+
test('we can create a new node', () => {
13+
const ll = new LinkedList();
14+
const data = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
15+
const node = new Node(data);
16+
17+
expect(ll).toBeDefined();
18+
expect(node.data).toEqual(data);
19+
});
20+
21+
test('we can append to the linked list', () => {
22+
const ll = new LinkedList();
23+
const data = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
24+
const node = new Node(data);
25+
ll.append(data);
26+
expect(ll.head).toEqual(node);
27+
});
28+
29+
test('we can prepend to the linked list', () => {
30+
const ll = new LinkedList();
31+
const firstData = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
32+
const secondData = {'repoName': 'jest', 'owner': 'facebook'};
33+
const firstNode = new Node(firstData);
34+
const secondNode = new Node(secondData);
35+
36+
ll.append(firstData);
37+
ll.prepend(secondData);
38+
secondNode.next = firstNode;
39+
40+
expect(ll.head).toEqual(secondNode)
41+
});
42+
43+
test('we can delete from the linked list', () => {
44+
const ll = new LinkedList();
45+
46+
const firstData = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
47+
const firstNode = new Node(firstData);
48+
49+
ll.append(firstData);
50+
expect(ll.head).toEqual(firstNode);
51+
52+
ll.deleteWithValue('repoName', 'JavaScriptAlgorithms');
53+
54+
expect(ll.head).toEqual(null);
55+
});
56+
57+
test('we receive false when trying to delete a non-existent node', () => {
58+
const ll = new LinkedList();
59+
expect (ll.deleteWithValue('repoName', 'nothing')).toBe(false);
60+
});
61+
62+
test('we receive true when successfully deleting a node', () => {
63+
const ll = new LinkedList();
64+
const firstData = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
65+
66+
ll.append(firstData);
67+
68+
expect (ll.deleteWithValue('repoName', 'JavaScriptAlgorithms')).toBe(true);
69+
});
70+
71+
test('we can retrieve a node from the linked list', () => {
72+
const ll = new LinkedList();
73+
74+
const firstData = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
75+
const secondData = {'repoName': 'jest', 'owner': 'facebook'};
76+
const secondNode = new Node(secondData);
77+
78+
ll.append(firstData);
79+
ll.append(secondData);
80+
81+
const fb = ll.getNodeWithValue('owner', 'facebook');
82+
83+
expect(fb).toEqual(secondNode);
84+
});
85+
86+
test('we receive null when retreiving a non-existent node', () => {
87+
const ll = new LinkedList();
88+
89+
const firstData = {'repoName': 'JavaScriptAlgorithms', 'owner': 'vJechsmayr'};
90+
const firstNode = new Node(firstData);
91+
92+
ll.append(firstData);
93+
94+
const search = ll.getNodeWithValue('repoName', 'nothing');
95+
96+
expect(search).toBe(null);
97+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Node {
2+
constructor (data) {
3+
this.data = data;
4+
this.next = null;
5+
}
6+
}
7+
8+
module.exports = {Node: Node}

0 commit comments

Comments
 (0)