Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nikhilmufc7
Created April 11, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilmufc7/a29c76ea1671b79a950c64ac657d98b6 to your computer and use it in GitHub Desktop.
Save nikhilmufc7/a29c76ea1671b79a950c64ac657d98b6 to your computer and use it in GitHub Desktop.
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