Python: Find a valid substring of s that contains matching brackets, at least one of which is nested
Python Programming Puzzles: Exercise-87 with Solution
Write a Python program to find a valid substring of a given string that contains matching brackets, at least one of which is nested.
Input: ]][][[]]] Output: [[]] Input: ]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]][[[[[[[[[[[[[[[[[[ Output: [[][][][]]
Pictorial Presentation:

Sample Solution:
Python Code:
#License: https://bit.ly/3oLErEI
def test(s):
import re
return re.search(r"\[(\[\])+\]", s).group(0)
brackets = "]][][[]]]"
print("Original List of strings:",brackets)
print("\nFind a valid substring of the said string that contains matching brackets, at least one of which is nested:")
print(test(brackets))
brackets = "]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]][[[[[[[[[[[[[[[[[["
print("\nOriginal List of strings:",brackets)
print("\nFind a valid substring of the said string that contains matching brackets, at least one of which is nested:")
print(test(brackets))
Sample Output:
Original List of strings: ]][][[]]] Find a valid substring of the said string that contains matching brackets, at least one of which is nested: [[]] Original List of strings: ]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]][[[[[[[[[[[[[[[[[[ Find a valid substring of the said string that contains matching brackets, at least one of which is nested: [[][][][]]
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the vowels from each of the original texts (y counts as a vowel at the end of the word).
Next: Find an integer with the given number of even and odd digits.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join