Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c2f3a52

Browse files
Diego Flores CastilloDiego Flores Castillo
Diego Flores Castillo
authored and
Diego Flores Castillo
committed
isValid-003 (feature): implement valid parentheses
1 parent 062fc88 commit c2f3a52

File tree

8 files changed

+180
-21
lines changed

8 files changed

+180
-21
lines changed

.idea/Algorithms-Leetcode-Javascript.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LeetcodeProblems/Algorithms/Valid_Parentheses.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,27 @@ Example 5:
3131
Input: "{[]}"
3232
Output: true
3333
*/
34-
35-
var isValid = function (s) {
36-
var stack = [];
37-
for (var i = 0; i < s.length; i++) {
38-
var elem = s.charAt(i);
39-
if (elem === ")" || elem === "]" || elem === "}") {
40-
if (stack.length === 0) return false;
41-
var lasPar = stack.shift();
42-
if (!valid(lasPar, elem)) return false;
43-
} else {
44-
stack.unshift(elem);
45-
}
34+
const closeParenthesesCaracter = {
35+
"(": ")",
36+
"{": "}",
37+
"[": "]"
38+
};
39+
const isValid = (parentheses) => {
40+
if (parentheses === "") {
41+
return true;
42+
}
43+
if (parentheses.length % 2 !== 0) return false;
44+
if (parentheses.length === 2) {
45+
return closeParenthesesCaracter[`${parentheses[0]}`] === parentheses[1];
4646
}
4747

48-
return stack.length === 0;
49-
};
48+
let firstParentheses = parentheses[0];
49+
let closeParentheses = parentheses.indexOf(closeParenthesesCaracter[firstParentheses]);
5050

51-
var valid = function (parOpen, parClose) {
52-
return (
53-
(parOpen === "(" && parClose === ")") ||
54-
(parOpen === "[" && parClose === "]") ||
55-
(parOpen === "{" && parClose === "}")
56-
);
57-
};
51+
if (closeParentheses === -1 || closeParentheses % 2 === 0) return false;
52+
53+
let nextParentheses = parentheses.slice(1, closeParentheses) + parentheses.slice(closeParentheses + 1);
54+
return isValid(nextParentheses);
5855

56+
};
5957
module.exports.isValid = isValid;

0 commit comments

Comments
 (0)