Java 使用递归生成给定字符串的所有子字符串

Java 使用递归生成给定字符串的所有子字符串,java,string,recursion,substring,Java,String,Recursion,Substring,首先,我应该说这里也有类似的问题,但对于我正在进行的作业,我不能使用任何循环,这些问题的所有答案都使用循环。因此,使用Java6和递归生成给定字符串的所有子字符串。例如,您给定的字符串word=“Ralph”;我需要像这样格式化输出 Ralph Ralp Ral Ra R alph alp al a lph lp l ph h 这是我的生成方法 //written by Justin Tew<BR> public static void generate(String w

首先,我应该说这里也有类似的问题,但对于我正在进行的作业,我不能使用任何循环,这些问题的所有答案都使用循环。因此,使用Java6和递归生成给定字符串的所有子字符串。例如,您给定的字符串word=“Ralph”;我需要像这样格式化输出

Ralph
Ralp
Ral
Ra
R
alph
alp
al
a
lph
lp
l
ph
h
这是我的生成方法

    //written by Justin Tew<BR>

public static void generate(String word) 
{


    //base case... wtf is the base case here?
    //idk bout this 
    if (word.length() == 1)
    {
        System.out.println(word);
        return;
    }


    //recursive case
    if (word.length() != 0)
    {

        System.out.println(word);
        generate(word.substring(0, word.length()-1)); //gets the first 5 substrings
    }
在我看来,这个调用,
generate(word.substring(1,word.length()-1))应该得到下一个5,但它没有得到非常奇怪的输出


有什么想法吗?

你可以根据单词的长度递归,而不是根据单词的字母递归。例如,在递归的顶层,您可以找到包含
word.length()
字母的所有子字符串,然后是
word.length()-1
字母等等。不过,这可能需要两个递归方法,一个循环遍历单词长度,另一个循环遍历该长度的所有可能的子字符串。

听起来您已经完成了大部分工作。只需编写另一个递归方法
generateSuffix(word)

  • 第一次调用
    generate(word)
  • 然后调用
    以单词的最长后缀生成uffix()

您仍然需要一个与generate中类似的基本情况。

这两个答案都非常正确。我刚刚添加了一个名为
subfixgen
的新方法:

public static void suffixGen(String word)
{
    if (word.length() > 1)
    {
        generate(word);
        suffixGen(word.substring(1));
    }

}

在我的主要过程中,我只需调用
suffixGen
而不是
generate
,它就能得到所需的结果。

您不需要助手方法,如果向方法传递额外的字符串,只需将其值作为空白传递,如下面的方法调用所示:

    public static void substrings(String str, String temp)
    {
        if(str.length()==0)
        {
            System.out.println(temp); return;
        }

          substrings(str.substring(1), temp+str.substring(0,1));
          substrings(str.substring(1), temp);
    }
一个示例调用-->子字符串(“abc”和“)

生成以下输出:

abc

ab

交流电

a

卑诗省

b

c


有一个不可见的字符串实际上是一个空白字符串。

试试这样的方法

String word; 
int word_length = word.length(); //get the length of the word

for(int i=0;i<word_length;i++){
   for(int j=0; j<=word_length-i ; j++){

       String sub = word.substring(i,i+j); 
       System.out.println(sub); //print the substrings
    }
字符串字;
int word_length=word.length()//获取单词的长度

对于(inti=0;i,这里是一个易于阅读的解决方案

public class AllSubStrings {
    //hashset to keep a record of all the substrings
    static HashSet<String> subStrings_r=new HashSet<>();

    public static void main(String[] args) {
        String testString="Sujal";
        getSubstrings_r(testString);
        System.out.println("RECURSION ->"+subStrings_r);
    }

    public static void getSubstrings_r(String testString){
        _getSubstrings_r(testString, 0, testString.length());
    }

    public static void _getSubstrings_r(String testString,int start,int end){
        if(start==end){ //base condition
            return;
        }
        subStrings_r.add(testString.substring(start, end));
        //start getting substrings from left to right
        _getSubstrings_r(testString,start+1,end); 
        //start getting substrings from right to left
        _getSubstrings_r(testString,start,end-1);
    }

}
公共类所有子字符串{
//hashset保存所有子字符串的记录
静态HashSet substring_r=新HashSet();
公共静态void main(字符串[]args){
String testString=“Sujal”;
获取子字符串(testString);
System.out.println(“递归->”+子字符串);
}
公共静态void getSubstrings\u r(String testString){
_getsubstring_r(testString,0,testString.length());
}
公共静态void\u getsubstring\u r(字符串testString,int start,int end){
如果(开始==结束){//基本条件
返回;
}
子字符串\u r.add(testString.substring(start,end));
//从左到右开始获取子字符串
_getSubstrings\u r(testString,start+1,end);
//从右到左开始获取子字符串
_getsubstring_r(testString,start,end-1);
}
}

make因为今天放学后要尝试它,所以它似乎不起作用。
ac
不是
abc
的子字符串。
public class AllSubStrings {
    //hashset to keep a record of all the substrings
    static HashSet<String> subStrings_r=new HashSet<>();

    public static void main(String[] args) {
        String testString="Sujal";
        getSubstrings_r(testString);
        System.out.println("RECURSION ->"+subStrings_r);
    }

    public static void getSubstrings_r(String testString){
        _getSubstrings_r(testString, 0, testString.length());
    }

    public static void _getSubstrings_r(String testString,int start,int end){
        if(start==end){ //base condition
            return;
        }
        subStrings_r.add(testString.substring(start, end));
        //start getting substrings from left to right
        _getSubstrings_r(testString,start+1,end); 
        //start getting substrings from right to left
        _getSubstrings_r(testString,start,end-1);
    }

}