Tuesday, May 30, 2023
HomeSoftware EngineeringTake away the parentheses in Java

Take away the parentheses in Java


The problem

On this problem you’re given a string for instance:

"instance(undesirable factor)instance"

Your activity is to take away every part contained in the parentheses in addition to the parentheses themselves.

The instance above would return:

"exampleexample"

Notes

  • Apart from parentheses solely letters and areas can happen within the string. Don’t fear about different brackets like "[]" and "{}" as these won’t ever seem.
  • There may be a number of parentheses.
  • The parentheses may be nested.

The answer in Java code

Choice 1:

public class Resolution {
    public static String removeParentheses(ultimate String str) {
        String up to date = str.replaceAll("([^()]*)", "");
        if (up to date.accommodates("(")) up to date = removeParentheses(up to date);     
        return up to date;
    }
}

Choice 2:

public class Resolution {
    public static String removeParentheses(ultimate String str) {
        
    String res = "";
    int rely = 0;
    for (int l = 0; l < str.size(); l++) {
        char c = str.charAt(l);
        if (c == '(') rely += 1;
        if (rely == 0) res += str.charAt(l);
        if (c == ')') rely -= 1;
    }
    return res;
    }
}

Choice 3:

public class Resolution {
    public static String removeParentheses(ultimate String str) {
        String end result = str;

        whereas (end result.accommodates("(")) {
            end result = end result.replaceAll("([^()]*)", "");
        }
        return end result;
    }
}

Check instances to validate our resolution

import org.junit.Check;
import java.util.*;
import java.util.stream.*;

public class SolutionTest {
    @Check
    public void fixedTests() {
        for (String[] trial : new String[][]{
                {"instance(undesirable factor)instance", "exampleexample"},
                {"instance(undesirable factor)instance", "exampleexample"},
                {"instance (undesirable factor) instance", "instance  instance"},
                {"a (bc d)e", "a e"},
                {"a(b(c))", "a"},
                {"hiya instance (phrases(extra phrases) right here) one thing", "hiya instance  one thing"},
                {"(first group) (second group) (third group)", "  "}})
            Tester.doTest(trial[0], trial[1]);
    }
    personal static ultimate String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ   ";
    personal static ultimate Random rand = new Random();
    personal static String randomLetter() {
        int i = rand.nextInt(letters.size());
        return letters.substring(i, i+1);
    }
    @Check
    public void randomTests() {
        for (int trial = 1; trial <= 100; trial++) {
            String str = IntStream.vary(0, rand.nextInt(200)+2)
                                  .mapToObj(i -> randomLetter())
                                  .acquire(Collectors.becoming a member of(""));
            for (int parens = rand.nextInt(str.size()/2); 0 < parens--;) {
                int open = rand.nextInt(str.size()-1), shut = rand.nextInt(str.size()-open) + open;
                str = str.substring(0, open) + "(" + str.substring(open, shut) + ")" + str.substring(shut);
            }
            Tester.doTest(str, resolution(str));
        }
    }
    personal String resolution(ultimate String str) {
        StringBuilder sb = new StringBuilder();
        int depth = 0;
        for (char c : str.toCharArray()) {
            depth += '(' == c ? 1 : 0;
            if ( 0 == depth ) sb.append(c);
            depth -= ')' == c ? 1 : 0;
        }
        return sb.toString();
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments