Created
April 11, 2021 20:45
-
-
Save nikhilmufc7/a29c76ea1671b79a950c64ac657d98b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
isProperlyNested(String str) { | |
var splittedString = str.split(""); | |
List outputs = []; | |
for (var i = 0; i < str.length; i++) { | |
var currentVal = str[i]; | |
if (currentVal == '{' || currentVal == '(' || currentVal == '[') { | |
outputs.add(currentVal); | |
} else if (currentVal == '}' || currentVal == ')' || currentVal == ']') { | |
var t = outputs.removeLast() + currentVal; | |
if (t != "{}" && t != "()" && t != "[]") return 'Not properly nested'; | |
} else { | |
return 'Not properly nested'; | |
} | |
} | |
if (outputs.isNotEmpty) { | |
return 'Not properly nested'; | |
} else { | |
return 'String is properly nested'; | |
} | |
} | |
print(isProperlyNested("(){){(")); | |
print(isProperlyNested("({[]})")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment