简体   繁体   English

递归 - 将括号设置为字符串字符

[英]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))) .同样重要的是,不应有括号直接包含另一对括号,例如((a))b(((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 String的方法。 Class Brexit includes a method called append. Class 英国脱欧包括一个名为 append 的方法。 With this method I can append a String to the end of my array.使用这种方法,我可以 append 一个字符串到我的数组末尾。 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.由于Java 9你可以使用String.codePoints方法得到一个stream超过字符,然后使用mapreduce的方法。

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:然后得到这些 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: Java代码:

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:最终 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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