Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 735377c

Browse files
committedFeb 5, 2023
prettier
1 parent d06b420 commit 735377c

10 files changed

+168
-35
lines changed
 

‎.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

‎13romanToInteger.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
let hash = {
6+
I: 1,
7+
V: 5,
8+
X: 10,
9+
L: 50,
10+
C: 100,
11+
D: 500,
12+
M: 1000,
13+
};
14+
var romanToInt = function (s) {
15+
let number = 0;
16+
for (let i = s.length - 1; i > -1; i--) {
17+
let char = s[i];
18+
let prev = s[i - 1];
19+
let two = prev + char;
20+
switch (two) {
21+
case "IV":
22+
number += 4;
23+
i--;
24+
break;
25+
case "IX":
26+
number += 9;
27+
i--;
28+
break;
29+
case "XL":
30+
number += 40;
31+
i--;
32+
break;
33+
case "XC":
34+
number += 90;
35+
i--;
36+
break;
37+
case "CD":
38+
number += 400;
39+
i--;
40+
break;
41+
case "CM":
42+
number += 900;
43+
i--;
44+
break;
45+
default:
46+
number += hash[char];
47+
break;
48+
}
49+
}
50+
return number;
51+
};

‎1480runningSumOf1dArray.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* @param {number[]} nums
66
* @return {number[]}
77
*/
8-
var runningSum = function(nums) {
9-
for (let i=0;i<nums.length;i++) {
10-
nums[i] = nums[i] + (nums[i-1]||0)
11-
}
12-
return nums;
13-
};
8+
var runningSum = function (nums) {
9+
for (let i = 0; i < nums.length; i++) {
10+
nums[i] = nums[i] + (nums[i - 1] || 0);
11+
}
12+
return nums;
13+
};

‎205isomorphicString.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
* Two strings s and t are isomorphic if the characters in s can be replaced to get t.
55
* All occurrences of a character must be replaced with another character while preserving
66
* the order of characters. No two characters may map to the same character, but a
7-
* character may map to itself.
7+
* character may map to itself.
88
* @param {string} s - renamed to a
99
* @param {string} t - renamed to b
1010
* @return {boolean}
1111
*/
1212
var isIsomorphic = function (a, b) {
1313
if (a.length !== b.length) return false;
1414

15-
let aX = "", bX = "", hashForA = [], hashForB = [], aCounter = 0, bCounter = 0;
15+
let aX = "",
16+
bX = "",
17+
hashForA = [],
18+
hashForB = [],
19+
aCounter = 0,
20+
bCounter = 0;
1621
for (let i = 0; i < a.length; i++) {
17-
let charA = a[i], charB = b[i];
22+
let charA = a[i],
23+
charB = b[i];
1824

1925
if (hashForA[charA] == undefined) {
2026
hashForA[charA] = aCounter;
@@ -36,16 +42,18 @@ var isIsomorphic = function (a, b) {
3642
var isIsomorphic = (a, b) => {
3743
if (a.length !== b.length) return false;
3844

39-
let a2b = [], b2a = [];
45+
let a2b = [],
46+
b2a = [];
4047
for (let i = 0; i < a.length; i++) {
41-
let aChar = a[i], bChar = b[i];
42-
if(
43-
(a2b[aChar] || b2a[bChar])
44-
&&
45-
(a2b[aChar] !== bChar || b2a[bChar] !== aChar)
46-
) return false;
47-
if (!a2b[aChar]) a2b[aChar]=bChar;
48-
if (!b2a[bChar]) b2a[bChar]=aChar;
48+
let aChar = a[i],
49+
bChar = b[i];
50+
if (
51+
(a2b[aChar] || b2a[bChar]) &&
52+
(a2b[aChar] !== bChar || b2a[bChar] !== aChar)
53+
)
54+
return false;
55+
if (!a2b[aChar]) a2b[aChar] = bChar;
56+
if (!b2a[bChar]) b2a[bChar] = aChar;
4957
}
5058
return true;
51-
};
59+
};

‎21mergeTwoSortedLists.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
* @return {ListNode}
1717
*/
1818
var mergeTwoLists = function (a, b) {
19-
if (!a ?? !b) return a ? a : b
19+
if (!a ?? !b) return a ? a : b;
2020
// Make left less than or equal to right
21-
let left = null, right = null;
22-
if (a.val <= b.val) { left = a, right = b; }
23-
else { left = b, right = a; }
21+
let left = null,
22+
right = null;
23+
if (a.val <= b.val) {
24+
(left = a), (right = b);
25+
} else {
26+
(left = b), (right = a);
27+
}
2428

2529
// Store head for end
2630
let head = left;
@@ -29,7 +33,7 @@ var mergeTwoLists = function (a, b) {
2933
while (left && right) {
3034
if (left?.next?.val <= right?.val) {
3135
// Move forward
32-
left = left.next
36+
left = left.next;
3337
} else {
3438
// Unshift right node into left list
3539
left.next = new ListNode(right.val, left.next);
@@ -40,4 +44,4 @@ var mergeTwoLists = function (a, b) {
4044

4145
// Return new head node of merged lists,
4246
return head;
43-
};
47+
};

‎22generateParentheses.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
* @param {number} n
66
* @return {string[]}
77
*/
8-
var generateParenthesis = function(n) {
9-
10-
};
8+
var generateParenthesis = function (n) {
9+
// Edge cases
10+
if (n < 1) return [];
11+
if (n == 1) return ["()"];
12+
13+
let solutions = [];
14+
15+
// Closures
16+
let nextLevel = generateParenthesis(n - 1);
17+
solutions.push(...nextLevel.map((a) => "(" + a + ")"));
18+
solutions.push(...nextLevel.map((a) => "()" + a));
19+
nextLevel.forEach((a) => {
20+
if (!isBalanced(a)) solutions.push(a + "()");
21+
});
22+
23+
return solutions;
24+
};
25+
function isBalanced(s) {
26+
return s.replaceAll("()", "") == "";
27+
}

‎392isSubsequence.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
var isSubsequence = function (s, t) {
1212
let cursor = 0;
1313
for (let i = 0; i < t.length; i++) {
14-
if (t[i] == s[cursor]) cursor++
14+
if (t[i] == s[cursor]) cursor++;
1515
}
1616
return cursor == s.length;
17-
};
17+
};

‎724findPivotIndex.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
* @return {number}
1111
*/
1212
var pivotIndex = (nums) => {
13-
let prefixSum = [], postfixSum = [];
13+
let prefixSum = [],
14+
postfixSum = [];
1415
for (let i = 0; i < nums.length; i++) {
1516
let j = nums.length - 1 - i;
16-
postfixSum[j] = (postfixSum[j + 1] || 0) + nums[j]
17-
prefixSum[i] = (prefixSum[i - 1] || 0) + nums[i]
17+
postfixSum[j] = (postfixSum[j + 1] || 0) + nums[j];
18+
prefixSum[i] = (prefixSum[i - 1] || 0) + nums[i];
1819
}
1920
for (let i = 0; i < nums.length; i++) {
2021
let j = nums.length - 1 - i;
2122
if ((prefixSum[i - 1] || 0) == (postfixSum[i + 1] || 0)) return i;
2223
}
2324
return -1;
24-
};
25+
};

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# algorithms-and-data-structures
2+
23
Studying data structures &amp; algorithms
34

45
[1480. Running Sum of 1d Array](1480runningSumOf1dArray.js)
@@ -9,4 +10,4 @@ Studying data structures &amp; algorithms
910

1011
[392. Is Subsequence](392isSubsequence.js)
1112

12-
[21. Merge Two Sorted Lists](21mergeTwoSortedLists.js)
13+
[21. Merge Two Sorted Lists](21mergeTwoSortedLists.js)
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Page 70
2+
* 'There is an O(N) algorithm.'
3+
* Given a smaller string s and a bigger string b, design an
4+
* algorithm to find all permutations of the shorter string
5+
* within the longer one. Print the location each permutation.
6+
*/
7+
const findPermutations = (needle, haystack) => {
8+
// Store a array of location solutions
9+
let locations = [];
10+
11+
// This is a named constraint that helps us get to O(haystack)
12+
// instead of O(needle) because: needle < haystack = true
13+
if (needle.length > haystack.length) console.error("But the book promised!!");
14+
15+
// Let countmap
16+
let needleHash = [];
17+
18+
// Loop through the haystack and make a hash
19+
for (let i = 0; i < needle.length; i++) {
20+
// Get the current character at this haystack position
21+
let char = neelde[i];
22+
23+
//
24+
if (!needleHash[char]) needleHash[char] = [];
25+
needleHash[char][i] = true;
26+
}
27+
28+
// Loop through again for cursor/counter magic
29+
for (let i = 0; i < haystack.length; i++) {
30+
// Get the current character at this haystack position
31+
let char = haystack[i];
32+
33+
// If this character is present in the needle...
34+
if (hash[char]) {
35+
// Start a cursor at this positions
36+
cursor[i];
37+
}
38+
}
39+
40+
// locations = haystackCounterHash.filter()
41+
console.log({ hash });
42+
43+
return locations;
44+
};
45+
46+
// Example from book
47+
let needle = "abbc";
48+
let haystack = "cbabadcbbabbcbabaabccbabc";
49+
let result = findPermutations(needle, haystack);
50+
console.log(result);

0 commit comments

Comments
 (0)
Please sign in to comment.