java中句子中子字符串的所有组合

java中句子中子字符串的所有组合,java,algorithm,data-structures,Java,Algorithm,Data Structures,我需要在一个空格分隔的字符串中找到单词的组合。假设字符串为“I will go”,则输出为- 一、 威尔,走 一、 会去的 我会的,走 绳子可能更大。 我试过了,但是卡住了。你能帮我解决这个问题吗。 提前谢谢 public class Combination_Of_Words_In_Sentence { public static void main(String[] args) { String inputString = "I will go"; String[] arrS

我需要在一个空格分隔的字符串中找到单词的组合。假设字符串为“I will go”,则输出为-

  • 一、 威尔,走
  • 一、 会去的
  • 我会的,走
  • 绳子可能更大。 我试过了,但是卡住了。你能帮我解决这个问题吗。 提前谢谢

    public class Combination_Of_Words_In_Sentence {
    
    public static void main(String[] args) {
        String inputString = "I will go";
        String[] arrString = inputString.split(" ");
        printString(arrString);
    }
    
    private static void printString(String[] arrString) {
        int len = arrString.length;
        String[] arr = new String[len];
    
        for(int i=0;i<arrString.length;i++){
            for(int j=0;j<i+1;j++){
                arr[i] = arrString[j]+" ";
                System.out.println();
            }
            arr[i] +=",";
            printPatternUtil(arrString, arr, 1, 1, len);
        }   
    }
    
    private static void printPatternUtil(String[] arrString, String[] arr,
            int i, int j, int n) {
        if(i == n){
            // arr[j] = " ";
             for(int k=0;k<arr.length;k++)
             System.out.print(arr[k]);
    
             System.out.println();
                return;
        }
        arr[j] = arrString[i]+",";
        printPatternUtil(arrString, arr, i+1, j+1, n) ;
    
        // Or put a space followed by next character
       // arr[j] = ",";
        //arr[j+1] = arrString[i]+ " ";
    
       // printPatternUtil(arrString, arr, i+1, j+2, n);
    
    }
    
    }
    
    句子中单词的公共类组合{ 公共静态void main(字符串[]args){ String inputString=“我会去”; String[]arrString=inputString.split(“”); printString(arrString); } 私有静态void打印字符串(字符串[]arrString){ int len=arrString.length; 字符串[]arr=新字符串[len];
    对于(inti=0;i我建议您使用按位解决方案,以避免需要递归。 您可以用二进制数计算所需的组合数。 例如,对于两个单词,您需要一个逗号。 三个单词需要两个逗号。 因此,逗号数=字数-1

    我们可以用计数器中的位来表示逗号

    • 第一个逗号=位0
    • 第二个逗号=第1位
    • 第三个逗号=第2位
    • 等等
    所以,对于两个逗号,我们需要2位。 2位的可能组合为:

    • 0b00=0
    • 0b01=1
    • 0b10=2
    • 0b11=3
    对于三个逗号,可能的组合为0b000=0、0b001=1、0b010=2、0b011=3、0b100=4、0b101=5、0b110=6和0b111=7

    因此,对于两个逗号,您只需要从0(0b00)到3(0b11)进行计数,并测试每个位是否需要插入逗号。对于三个逗号,从0(0b000)到7(0b111)进行计数

    这很容易计算。对于2个逗号,取2的幂2=4。对于3个逗号,取2的幂3=8

    String[] words = {...};
    int wordcount = words.length;
    int commacount = wordcount - 1;
    // Calculate the highest number to count to, in our loop
    int looplimit = 1 << commacount; // Same as 2 to the power commacount;
    for(int i=0;i<looplimit;i++)
    {
        // Start this phrase version with the first word.
        String result = words[0];
    
        // Add the rest of the words, optionally adding commas.
        // We've already added a word, so only count to wordcount-1
        for(int j = 0; j<wordcount-1;j++)
        {
            // For word j, test the loop counter (i) to see if bit j is set.
            boolean needComma = (i & (1 << j)) != 0;
    
            // Add a comma or a space to this phrase version
            result += needComma ? "," : " ";
    
            // Add this word to the phrase version
            result += words[j+1];
        }
        System.out.println(result);
    }
    
    String[]words={…};
    int wordcount=words.length;
    int commacount=wordcount-1;
    //计算循环中要计数的最大数字
    
    int looplimit=1不清楚您想要什么。您必须在不同的位置放置逗号?嗨,卢卡,谢谢您的回复。我需要字符串列表。列表1->[I,will,go],列表2->[I,will go]列表3->[I,go]是的。那也可以。请看这里:我会先把句子分解成单独的单词。然后写一些递归解决方案。我脑子里还没有计算出细节,它们对于堆栈溢出格式来说太多了。