Java enclosing parentheses id

I would really appreciate it if you could help me with this in Java.

Given two lines, say String A = "(A+B)+(C)"

and String B = "((A+B)+(C))"

and String C = (A+B)

and String D = A+(B+C)

and andString E = (A+(B+C))

How can I tell if a String is completely surrounded by a parenthesis like String B.

In the example: boolean flag(String expr) { //return false if surrounded, else true }

If expr = A

, the flag will return true

If expr = B

, the flag will return false

If expr = C

, the flag will return false

If expr = D

, the flag will return true

If expr = E

, the flag will return flase

Sorry if it's not clear, but it should work for any String expression:

Suppose the expression contains only Numbers and Operators and Parenthesis .

Thank. Appreciate this.

+3


source to share


3 answers


You cannot do this * with a regex, because nested parentheses are not common language.

Instead, iterate over the string and track the nesting level by counting the number of opening and closing parentheses. Add one nesting level for each open parenthesis. For each closing parenthesis, subtract one.

  • If you reach zero (or less) before reaching the end of the line, return true.
  • If you reach zero at the end, return false.
  • Anything else is unbalanced parentheses and shouldn't happen if your input is invalid.


Here are some examples to demonstrate the principle:

(A+B)+(C)
11110        TRUE

((A+B)+(C))
12222112210  FALSE

(A+B)
11110        FALSE

A+(B+C)
0            TRUE

(A+(B+C))
111222210    FALSE

      

* healthy

+4


source


In your situation, I see 2 options.

  • Use substring method

Example:



public boolean checkForParanthesis(String str) {
 Integer last = str.length() - 1; // Get number of the last character
 String firstChar = str.substring(0); // Get first character of the string
 String lastChar = str.substring(last); // Get last character of the string
 if (firstChar.equals("(") && lastChar.equals(")")) return false;
 return true
}

      

  • Use the reqular expression. Perhaps this is the best solution.
+1


source


Mark Byers' algorithm seems to roughly match what you are looking for. Now, to compose this, you have to play with keywords and indexes for

and if

Java. An example would be the following code. It doesn't check the expression, however, so no error is thrown when, for example, an invalid expression is checked A+B)

(it just returns a value true

). Check it out and check it yourself. Hope this helps a little ...


package test;

public class Main {

  public static void main(String[] args) {
    Main m = new Main();
    m.start();
  }

  private void start() {
    /* true */
    System.out.println(isNotSurrounded("A"));
    System.out.println(isNotSurrounded("A+B"));
    System.out.println(isNotSurrounded("A+(B+C)"));
    System.out.println(isNotSurrounded("(B+C)+D"));
    System.out.println(isNotSurrounded("A+(B+C)+D"));
    System.out.println(isNotSurrounded("(A+B)+(C)"));
    System.out.println(isNotSurrounded("(A)+(B)+(C)"));
    System.out.println(isNotSurrounded("(A)+((B)+(C))+(D+E+F+(G))"));
    /* false */
    System.out.println();
    System.out.println(isNotSurrounded("(A)"));
    System.out.println(isNotSurrounded("(A+B)"));
    System.out.println(isNotSurrounded("(A+(B+C))"));
    System.out.println(isNotSurrounded("((B+C)+D)"));
    System.out.println(isNotSurrounded("(A+(B+C)+D)"));
    System.out.println(isNotSurrounded("((A+B)+(C))"));
    System.out.println(isNotSurrounded("((A)+(B)+(C))"));
    System.out.println(isNotSurrounded("((A)+((B)+(C))+(D+E+F+(G)))"));
  }

  private boolean isNotSurrounded(String expression) {
    if (expression.startsWith("(") && expression.endsWith(")") && expression.length() > 2) {
      int p = 0;
      for (int i = 1; i < expression.length() - 1; i++) {
        if (expression.charAt(i) == '(') {
          p++;
        } else if (expression.charAt(i) == ')') {
          p--;
        }
        if (p < 0) {
          return true;
        }
      }
      if (p == 0) {
        return false;
      }
    }
    return true;
  }
}

      

The code output looks like this:


true
true
true
true
true
true
true
true

false
false
false
false
false
false
false
false

      

+1


source







All Articles