package nl.dvberkel.dyck;

public class Checker {
    public boolean check(String word) {
        boolean valid = imbalance(word) == 0;
        int prefixLength = word.length() - 1;
        while (valid && prefixLength > 0) {
            String prefix = word.substring(0, prefixLength);
            valid = imbalance(prefix) >= 0;
            prefixLength--;
        }
        return valid;
    }

    private int imbalance(String word) {
        return count(word, "1") - count(word, "2");
    }

    private int count(String word, String substring) {
        int count = 0;
        for (int index = 0; index < word.length(); index++) {
            if (word.substring(index, index + substring.length()).equals(substring)) {
                count++;
            }
        }
        return count;
    }
}

@@ -43,16 +43,244 @@
 ecker %7B%0A
+    private final String left;%0A    private final String right;%0A%0A    public Checker() %7B%0A        this(%221%22, %222%22);%0A    %7D%0A%0A    public Checker(String left, String right) %7B%0A%0A        this.left = left;%0A        this.right = right;%0A    %7D%0A%0A
     publ
@@ -687,19 +687,20 @@
 t(word, 
-%221%22
+left
 ) - coun
@@ -707,19 +707,21 @@
 t(word, 
-%222%22
+right
 );%0A    %7D

Allow for other delimiters