From 5267c1fd88d22a378c97e72268793e66289fd500 Mon Sep 17 00:00:00 2001 From: Anthony C Date: Tue, 1 Aug 2017 15:27:44 -0400 Subject: [PATCH] Kotlin - Completed BrackedPush --- .../src/main/kotlin/BracketPush.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 kotlin/bracket-push/src/main/kotlin/BracketPush.kt diff --git a/kotlin/bracket-push/src/main/kotlin/BracketPush.kt b/kotlin/bracket-push/src/main/kotlin/BracketPush.kt new file mode 100644 index 0000000..c2da8bb --- /dev/null +++ b/kotlin/bracket-push/src/main/kotlin/BracketPush.kt @@ -0,0 +1,32 @@ +class BracketPush { + companion object { + fun isValid(inpString: String): Boolean{ + val stack = mutableListOf() + val realString = inpString.filter { + (it == '{') or + (it == '[') or + (it == '(') or + (it == ')') or + (it == ']') or + (it == '}') } + for (i in realString){ + if ((i == '{') or + (i == '[') or + (i == '(')){ + stack.add(0, i) + } + else { + val temp = if (stack.size == 0) {'a'} else {stack.removeAt(0)} + when (temp){ + '(' -> if (i != ')') return false + '{' -> if (i != '}') return false + '[' -> if (i != ']') return false + else -> return false + } + } + } + return stack.isEmpty() + } + + } +} \ No newline at end of file