Skip to content

Commit 5530dcb

Browse files
Added solution for paranthesis.
1 parent 53c4e2d commit 5530dcb

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Happy to accept any contributions to this in any langugage.
3030
17. Rearrange Sorted Max Min | [Solution](./level_easy/Rearrange_Sorted_Max_Min.js)
3131
18. String Duplicates | [Solution](./level_easy/String-Duplicates.js)
3232
19. [String Reversal](./level_easy/string-reversal-README.md) | [Solution](./recursive_pattern/StringReverse_Recursive.js)
33-
33+
20. Valid Paranthesis | [Solution 1](./level_easy/Paranthesis_Mismatch.ts)
3434

3535
## Medium
3636
1. [Minimum Remove to Make Valid Paranthesis](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [Solution](./level_medium/LeetCode_1249_Medium.ts)
@@ -49,6 +49,7 @@ Happy to accept any contributions to this in any langugage.
4949
14. [Leet Code 200 Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Solution 1 DFS](./level_medium/LeetCode_200_NumberOfIslands.ts) | [Solution BFS](./level_medium/LeetCode_200_NumberOfIslands_1.ts) | [Solution Union Find](./level_medium/LeetCode_200_NumberOfIslands_2.ts)
5050
15. [Leet Code 362 Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Solution 1](./level_medium/LeetCode_362_Hit_Counter_1.ts) | [Solution 2](./level_medium/LeetCode_362_Hit_Counter_Medium.ts)
5151
16. [LeetCode Web Crawler 1242](https://leetcode.com/problems/web-crawler-multithreaded/) | [Solution 1 ](./level_medium/LeetCode_WebCrawler_MultiThreaded_1242.java) | [Solution 2](level_medium/LeetCode_WebCrawler_MultiThreaded_1242_1.java)
52+
17. [Leet Code 554 Brick Wall](https://leetcode.com/problems/brick-wall/) | [Solution 1](./level_medium/LeetCode_554_Brick_Wall_Medium.ts)
5253

5354

5455
## Hard

level_easy/Paranthesis_Mismatch.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Returns true if the string contains equal opening and closing paranthesis in the right order.
3+
* The order of the paranthesis should also be maintained.
4+
*
5+
* @param str
6+
*/
7+
function isValidParanethesis(str: string) {
8+
// Setting up a counter for frequency count of the paranthesis.
9+
// For each type of paranthesis, one counter is maintained.
10+
const paranthCount: number[] = [0, 0, 0];
11+
12+
// For each braces, we measure 2 things to see if the order and numbers are same:
13+
// If given, positive weight to opening and negative weight to closing braces, we have total 0 weight at end.
14+
// If at any step of iteration, the weight didn't go negative. This is essential bcz closing brace cannot come before negative.
15+
16+
// As seen in the example, this does not work if the relative order of the braces also needs to be tracked.
17+
// Possible solution for that is to use stack based approach of pulling items from the stack and compare.
18+
for(let idx = 0; idx < str.length; idx++) {
19+
const elem = str[idx]
20+
switch (elem) {
21+
case "{":
22+
paranthCount[0]++
23+
break
24+
case "}":
25+
paranthCount[0]--
26+
break
27+
case "(":
28+
paranthCount[1]++
29+
break
30+
case ")":
31+
paranthCount[1]--
32+
break
33+
case "[":
34+
paranthCount[2]++
35+
break
36+
case "]":
37+
paranthCount[2]--
38+
break
39+
default:
40+
break
41+
}
42+
43+
if (paranthCount[0] < 0 || paranthCount[1] < 0 || paranthCount[2] < 0) return false;
44+
}
45+
46+
return paranthCount[0] === 0 && paranthCount[1] === 0 && paranthCount[2] === 0;
47+
}
48+
49+
function testParanthMismatch(str: string) {
50+
console.log(`Paranthesis for string ${str} is correct: ${isValidParanethesis(str)}`);
51+
}
52+
53+
54+
testParanthMismatch("(((")
55+
testParanthMismatch("((()))")
56+
testParanthMismatch("((())))")
57+
testParanthMismatch("[{}]")
58+
testParanthMismatch("[{]}")
59+
60+
/**
61+
* This is incorrect, this technique only works if we have only one character. Otherwise interleaving characters will
62+
* not give correct results.
63+
*
64+
*/
65+
testParanthMismatch("[{]}")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Problem: Leet Code 554.
3+
* There is a brick wall in front of you. Each brick has equal height but might have
4+
* different widths. The brick wall is placed with different bricks together,
5+
* such that the total width of the entire wall would be equal. Need to find the
6+
* draw a vertical line from the top to the bottom such that it passes the least
7+
* number of bricks.
8+
*
9+
* Complexity: Time - O(B) where B is the total number of bricks.
10+
* Complexity: Space - O(M) where M will atmost be the width of the wall(assuming brick size of 1.)
11+
*
12+
* Intuition: Frequency Pattern: At a basic level, at each width of size 1 of brick,
13+
* we need to figure out the frequency of open pathways. At an optimized level, the
14+
* frequency can be identified for each possible brick width, but not for each
15+
* width 1. This is a dictionary approach for figuring out the frequency.
16+
*
17+
* The pathways (or width size) with the maximum frequency for open pathways is the
18+
* solution for us.
19+
*
20+
* Solution: Solution calculates all the entry points at any given brick size
21+
* position. So, in the worst case if the bricks are of size 1, it will calculate
22+
* the possible entry point at each such position. For optimization, we just figure
23+
* out the entry point at only positions where we have a brick.
24+
*
25+
* @param wall
26+
*/
27+
function leastBricks(wall: number[][]): number {
28+
// Use of a map to add openings at each brick size end.
29+
const brickWidths: {[k: number]: number} = {}
30+
let maxOpenings = 0
31+
32+
// Iterate over all the bricks.
33+
for (let row = 0; row < wall.length; row++) {
34+
let width = 0;
35+
for (let col = 0; col < wall[row].length - 1;col++) {
36+
// For each brick, calculate the total width.
37+
width += wall[row][col]
38+
39+
// Increment the number of openings at each width size.
40+
brickWidths[width] = brickWidths[width] | 0
41+
brickWidths[width]++
42+
maxOpenings = Math.max(maxOpenings, brickWidths[width])
43+
}
44+
}
45+
46+
return wall.length - maxOpenings;
47+
};

0 commit comments

Comments
 (0)