xxxxxxxxxx
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
};
xxxxxxxxxx
let s = '([]{})()'
const isValid = function(s) {
let stack = []
let characters = '()[]{}' // even, odd, even, odd, even, odd
for (let i = 0; i < s.length; i++) {
let index = characters.indexOf(s[i])
if (index % 2 === 0) {
stack.push(s[i])
} else if (index % 2 !== 0 && stack[stack.length - 1] === characters[index - 1]) {
stack.pop()
} else {
return false
}
console.log(stack)
}
return !stack.length
}
console.log(isValid(s))
// [Log]:
[ '(' ]
[ '(', '[' ]
[ '(' ]
[ '(', '{' ]
[ '(' ]
[]
[ '(' ]
[]
true
xxxxxxxxxx
class Solution:
def isValid(self, s):
if s == "":
return True
if len(s) < 2:
return False
pair_brkts = {
"{" : "}",
"(" : ")",
"[" : "]"
}
stack = []
for i in s:
if i in pair_brkts:
stack.append(i) #stack i(forward facing brackets) that also exists as keys in our pair_brkts dictionary)
#print("forward facing brackets", stack) #to see the contents of your stacked list
else:
if len(stack) == 0 or pair_brkts[stack.pop()] != i: #if stack list is empty or the value pair of last
#list item isnt same, return False, otherwise break out of loop
#print("backward facing brackets", stack) #print to see whats left of your list after
#looping is over for all your i(i.e brackets)
return False
if len(stack) > 0: #if stack list is not empty at this point, return False, else return True
return False
return True
Task = Solution()
print("1. ", Task.isValid("({[()]})"))
print("2. ", Task.isValid("()[]{}"))
print("3. ", Task.isValid("(]"))
xxxxxxxxxx
class Solution:
def isValid (self, sequence: str):
'''
Function to pair if sequence contains valid parenthesis
:param sequence: Sequence of brackets
:return: True is sequence is valid else False
'''
stack = []
opening = set('([{')
closing = set(')]}')
pair = {')' : '(' , ']' : '[' , '}' : '{'}
for i in sequence :
if i in opening :
stack.append(i)
if i in closing :
if not stack :
return False
elif stack.pop() != pair[i] :
return False
else :
continue
if not stack :
return True
else :
return False
if __name__ == '__main__':
sequence = '{[()]}'
print(f'Is {sequence} valid ? : {Solution().isValid(sequence)}')
sequence1 = '{[()]}{]{}}'
print(f'Is {sequence1} valid ? : {Solution().isValid (sequence1)}')
xxxxxxxxxx
/*
n = 3
[ '()()()', '()(())', '(())()', '(()())', '((()))' ]
*/
const generateParenthesis = function (n) {
const res = [];
const dfs = function (left = n, right = n, combos = '') {
if (combos.length === 2*n) {
res.push(combos);
}
else if (left <= right) {
if (left > 0) {
dfs(left - 1, right, combos + '(');
}
if (right > 0) {
dfs(left, right - 1, combos + ')');
}
}
}
dfs();
return res;
}
console.log(generateParenthesis(3));
xxxxxxxxxx
{ { } [ ] [ [ [ ] ] ] } is VALID expression
[ [ [ ] ] ] is VALID sub-expression
{ } [ ] is VALID sub-expression
xxxxxxxxxx
import scala.collection.mutable.Stack
object ValidParentheses {
def isValid(s: String): Boolean = {
val stack = Stack[Char]()
val map = Map(')' -> '(', '}' -> '{', ']' -> '[')
for (char <- s) {
if (map.contains(char)) {
if (stack.isEmpty || stack.pop() != map(char))
return false
} else {
stack.push(char)
}
}
stack.isEmpty
}
def main(args: Array[String]): Unit = {
// Example usage
val input1 = "()[]{}" // Valid
val input2 = "([)]" // Invalid
val input3 = "{[]}" // Valid
println(s"Is '$input1' valid? ${isValid(input1)}")
println(s"Is '$input2' valid? ${isValid(input2)}")
println(s"Is '$input3' valid? ${isValid(input3)}")
}
}
xxxxxxxxxx
$structuredData = [
"@context" => "https://schema.org/",
"@type" => "JobPosting",
"title" => $post->post_title,
"description" => $fields['vacatures_functieomschrijving'][0],
"identifier" => array(
"@type" => "PropertyValue",
"name" => "[organization]_vacatures",
"value" => get_the_permalink($post->ID)
),
"datePosted" => $datePosted,
"employmentType" => $vacatureType,
"hiringOrganization" => array(
"@type" => "Organization",
"name" => 'Organization name',
"sameAs" => site_url(),
"logo" => get_theme_file_uri() . "/assets/images/logo.jpg"
),
"jobLocation" => array(
"address" => array(
"@type" => "PostalAddress",
"addressLocality" => $fields['vacatures_locatie'][0],
"addressRegion" => $fields['vacatures_regio'][0],
"streetAddress" => $fields['vacatures_straathuisnummer'][0],
"postalCode" => $fields['vacatures_postcode'][0],
"addressCountry" => get_locale()
)
),
"baseSalary" => array(
"@type" => "MonetaryAmount",
"currency" => "EUR",
"value" => array(
"@type" => "QuantitativeValue",
"minValue" => $baseSalaryMinValue,
"maxValue" => $baseSalaryMaxValue,
"unitText" => $baseSalaryUnit
)
)
];