简体   繁体   中英

Recursion - Setting brackets to string characters

I have a problem with a programming task. My job is to write a method that gets a string and brackets it. It should be returned in an array. For example:

String s = "ab";

Then the array has to be:

[ab, (a)b, ((a)(b)), a(b), (ab), (a)(b), ((a)b), (a(b))]

It is important that no result occurs twice. It is also important that there shouldnt be brackets directly enclosing another pair of brackets like ((a))b or (((a)(b))) . If it is empty or zero, the result must also be an empty array. () is not allowed. I'm am only allowed to use methods of the class String . Class Brexit includes a method called append. With this method I can append a String to the end of my array. Now this is my code so far, but I don't know how to continue.

public class Brackets {
    public static String[] bracket(BrExIt b, String s) {
        String[] array = new String[]{};
        if (s == null || s == "") {
            return array;
        }
        if (s.length() == 1) {
            array = b.append(array, s);
            array = b.append(array, "(" + s + ")");
            return array;
        } else {
            int a = 0;
            return bracket(b, s.substring(a + 1, s.length() - 1));
        }
    }
}

Since Java 9 you can use String.codePoints method to get a stream over the characters, and then use the map and reduce approach.

Example string:

String s = "ab";

First represent each letter as an array of this letter and this letter with brackets:

[a, (a)]
[b, (b)]

Then get the Cartesian product of these arrays:

ab
a(b)
(a)b
(a)(b)

If you want to get more brackets , repeat the first operation:

[ab, (ab)]
[a(b), (a(b))]
[(a)b, ((a)b)]
[(a)(b), ((a)(b))]

Java code:

String s = "ab";

String[] array = s
        // stream over the characters of the string
        .codePoints()
        // Stream<String>
        .mapToObj(Character::toString)
        // represent each letter as an array of
        // this letter and this letter with brackets
        .map(str -> new String[]{str, "(" + str + ")"})
        // intermediate output
        .peek(arr -> System.out.println(Arrays.toString(arr)))
        // stream of arrays to a single array
        .reduce((arr1, arr2) -> Arrays.stream(arr1)
                .flatMap(str1 -> Arrays.stream(arr2)
                        .map(str2 -> str1 + str2))
                .toArray(String[]::new))
        // Stream<String[]>
        .stream()
        // Stream<String>
        .flatMap(Arrays::stream)
        // intermediate output
        .peek(System.out::println)
        // represent each string as an array of
        // this string and this string with brackets
        .map(str -> new String[]{str, "(" + str + ")"})
        // intermediate output
        .peek(arr -> System.out.println(Arrays.toString(arr)))
        // Stream<String>
        .flatMap(Arrays::stream)
        .toArray(String[]::new);

// final output
System.out.println(Arrays.toString(array));

Final output:

[ab, (ab), a(b), (a(b)), (a)b, ((a)b), (a)(b), ((a)(b))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM