|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Check redundant brackets\n", |
| 8 | + "\n", |
| 9 | + "For a given expression in the form of a string, find if there exist any redundant brackets or not. It is given that the expression contains only rounded brackets or parenthesis and the input expression will always be balanced.\n", |
| 10 | + "A pair of the bracket is said to be redundant when a sub-expression is surrounded by unnecessary or needless brackets.\n", |
| 11 | + "\n", |
| 12 | + "Example:\n", |
| 13 | + "Expression: (a+b)+c\n", |
| 14 | + "Since there are no needless brackets, hence, the output must be 'false'.\n", |
| 15 | + "\n", |
| 16 | + "Expression: ((a+b))\n", |
| 17 | + "The expression can be reduced to (a+b). Hence the expression has redundant brackets and the output will be 'true'.\n", |
| 18 | + "\n", |
| 19 | + "Input Format :\n", |
| 20 | + "The first and the only line of input contains a string expression, without any spaces in between.\n", |
| 21 | + "Output Format :\n", |
| 22 | + "The first and the only line of output will print either 'true' or 'false'(without the quotes) denoting whether the input expression contains redundant brackets or not.\n", |
| 23 | + "\n", |
| 24 | + "Constraints:\n", |
| 25 | + "0 <= N <= 10^6\n", |
| 26 | + "Where N is the length of the expression.\n", |
| 27 | + "\n", |
| 28 | + "Time Limit: 1 second\n", |
| 29 | + "Sample Input 1:\n", |
| 30 | + "a+(b)+c\n", |
| 31 | + "Sample Output 1:\n", |
| 32 | + "true\n", |
| 33 | + "Explanation:\n", |
| 34 | + "The expression can be reduced to a+b+c. Hence, the brackets are redundant.\n", |
| 35 | + "Sample Input 2:\n", |
| 36 | + "(a+b)\n", |
| 37 | + "Sample Output 2:\n", |
| 38 | + "false" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 2, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "def checkRedundantBrackets(expression) :\n", |
| 48 | + " s=expression\n", |
| 49 | + " le = len(s)\n", |
| 50 | + " l=[]\n", |
| 51 | + " for i in range(le):\n", |
| 52 | + " if s[i]==\"(\":\n", |
| 53 | + " l.append(\"(\")\n", |
| 54 | + " elif s[i] in \"+-*/\":\n", |
| 55 | + " l.append(s[i])\n", |
| 56 | + " elif s[i] ==\")\":\n", |
| 57 | + " if l[-1]==\"(\":\n", |
| 58 | + " return True\n", |
| 59 | + " elif l[-1] in \"+-*/\":\n", |
| 60 | + " while l[-1]!=\"(\":\n", |
| 61 | + " l.pop()\n", |
| 62 | + " l.pop()\n", |
| 63 | + " return False\n", |
| 64 | + "\n" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 3, |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "name": "stdout", |
| 74 | + "output_type": "stream", |
| 75 | + "text": [ |
| 76 | + "true\n" |
| 77 | + ] |
| 78 | + } |
| 79 | + ], |
| 80 | + "source": [ |
| 81 | + "expression=\"(a)+b\"\n", |
| 82 | + "if checkRedundantBrackets(expression):\n", |
| 83 | + " print('true')\n", |
| 84 | + "else:\n", |
| 85 | + " print('false')" |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "metadata": { |
| 90 | + "kernelspec": { |
| 91 | + "display_name": "Python 3", |
| 92 | + "language": "python", |
| 93 | + "name": "python3" |
| 94 | + }, |
| 95 | + "language_info": { |
| 96 | + "codemirror_mode": { |
| 97 | + "name": "ipython", |
| 98 | + "version": 3 |
| 99 | + }, |
| 100 | + "file_extension": ".py", |
| 101 | + "mimetype": "text/x-python", |
| 102 | + "name": "python", |
| 103 | + "nbconvert_exporter": "python", |
| 104 | + "pygments_lexer": "ipython3", |
| 105 | + "version": "3.7.6" |
| 106 | + } |
| 107 | + }, |
| 108 | + "nbformat": 4, |
| 109 | + "nbformat_minor": 4 |
| 110 | +} |
0 commit comments