通过替换隐藏的“#"数字符号来生成所有可能的字符串组合 [英] Generate all possible string combinations by replacing the hidden “#” number sign

查看:82
本文介绍了通过替换隐藏的“#"数字符号来生成所有可能的字符串组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是生成该行的所有可能组合,而没有隐藏的#正方形.输入为XOXX#OO#XO,这是输出应为的示例:

My task is to generates all possible combinations of that rows without the hidden # square. The input is XOXX#OO#XO and here is the example of what the output should be:

XOXXOOOOXO 
XOXXOOOXXO 
XOXXXOOOXO 
XOXXXOOXXO

我只允许迭代地解决此解决方案,并且我不确定如何解决此问题,并且已经在此代码上工作了一个星期了.任何帮助将不胜感激!这是我的代码:

I am only allowed to solve this solution iteratively and I am not sure how to fix this and have been working on this code for a week now.. any help would be much appreciated! Here is my code:

import java.lang.Math;

public class help {
    public static void main(String[] args) {
        String str = new String("XOXX#OO#XO");
        UnHide(str);
    }

    public static void UnHide(String str) {
        //converting string to char 
        char[] chArr = str.toCharArray();
        //finding all combinations for XO 
        char[] xo = new char[]{'X', 'O'};

        int count = 0;
        char perm = 0;
        String s = "";

        //finding amount of times '#' appears in string
        for (int i = 0; i < str.length(); i++) {
            if (chArr[i] == '#')
                count++;
        }

        int[] combo = new int[count];
        int pMax = xo.length;

        while (combo[0] < pMax) {
            // print the current permutation
            for (int k = 0; k < count; k++) {
                //print each character
                //System.out.print(xo[combo[i]]);
                perm = xo[combo[k]];
                s = String.valueOf(perm);

                char[] xoArr = s.toCharArray();
                String strChar = new String(xoArr);
                //substituting '#' to XO combo
                for (int i = 0; i < chArr.length; i++) {
                    for (int j = 0; j < s.length(); j++) {
                        if (chArr[i] == '#') {
                            chArr[i] = xoArr[j];
                            strChar = String.copyValueOf(chArr);
                            i++;
                        }
                    }
                    i++;
                    if (i == chArr.length - 1) {
                        System.out.println(strChar);
                        i = 0;
                    }
                }
            }

            System.out.println(); //print end of line

            // increment combo
            combo[count - 1]++; // increment the last index
            //// if increment overflows
            for (int i = count - 1; combo[i] == pMax && i > 0; i--) {
                combo[i - 1]++;  // increment previous index
                combo[i] = 0;   // set current index to zero  
            }
        }
    }
}

推荐答案

您可以使用streams 迭代地生成所有可能的字符串组合,如下所示:

You can iteratively generate all possible combinations of strings using streams as follows:

public static String[] unHide(String str) {
    // an array of substrings around a 'number sign'
    String[] arr = str.split("#", -1);
    // an array of possible combinations
    return IntStream
            // iterate over array indices
            .range(0, arr.length)
            // append each substring with possible
            // combinations, except the last one
            // return Stream<String[]>
            .mapToObj(i -> i < arr.length - 1 ?
                    new String[]{arr[i] + "O", arr[i] + "X"} :
                    new String[]{arr[i]})
            // reduce stream of arrays to a single array
            // by sequentially multiplying array pairs
            .reduce((arr1, arr2) -> Arrays.stream(arr1)
                    .flatMap(str1 -> Arrays.stream(arr2)
                            .map(str2 -> str1 + str2))
                    .toArray(String[]::new))
            .orElse(null);
}

public static void main(String[] args) {
    // output to the markdown table
    String[] tests = {"XOXX#OOXO", "XOXX#OO#XO", "#XOXX#OOXO#", "XO#XX#OO#XO"};
    String header = String.join("</pre> | <pre>", tests);
    String matrices = Arrays.stream(tests)
            .map(test -> unHide(test))
            .map(arr -> String.join("<br>", arr))
            .collect(Collectors.joining("</pre> | <pre>"));

    System.out.println("| <pre>" + header + "</pre> |");
    System.out.println("|---|---|---|---|");
    System.out.println("| <pre>" + matrices + "</pre> |");
}

<身体>
XOXX#OOXO

XOXX#OO#XO

#XOXX#OOXO#

XO#XX#OO#XO

XOXXOOOXO
XOXXXOOXO

XOXXOOOOXO
XOXXOOOXXO
XOXXXOOOXO
XOXXXOOXXO

OXOXXOOOXOO
OXOXXOOOXOX
OXOXXXOOXOO
OXOXXXOOXOX
XXOXXOOOXOO
XXOXXOOOXOX
XXOXXXOOXOO
XXOXXXOOXOX

XOOXXOOOOXO
XOOXXOOOXXO
XOOXXXOOOXO
XOOXXXOOXXO
XOXXXOOOOXO
XOXXXOOOXXO
XOXXXXOOOXO
XOXXXXOOXXO

这篇关于通过替换隐藏的“#"数字符号来生成所有可能的字符串组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆