【问题标题】:Using recursion to generate all substrings of a given string使用递归生成给定字符串的所有子字符串
【发布时间】:2012-10-23 06:10:57
【问题描述】:

我应该首先说这里提出了类似的问题,但是对于我正在进行的作业,我不能使用任何循环,并且这些问题的所有答案都使用循环。所以使用 java 6 和递归来生成给定字符串的所有子字符串。例如,您给定的 String 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
    }

输出:

Ralph
Ralp
Ral
ra
r

在我看来,generate(word.substring(1, word.length()-1)); 应该得到下一个 5,但它不会得到非常奇怪的输出......

有什么想法吗?

【问题讨论】:

    标签: java string recursion substring


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      • 第一次调用generate(word)
      • 然后用最长的单词后缀调用generateSuffix()

      您仍然需要与您在 generate 中的基本案例类似的基本案例。

      【讨论】:

      • 今天放学后就试试看吧
      【解决方案3】:

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

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

      在我的主要工作中,我只调用suffixGen 而不是generate,它会得到我想要的结果。

      【讨论】:

        【解决方案4】:

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

            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);
            }
        

        一个示例调用 --->substrings("abc", "");

        产生以下输出:

        abc

        ab

        交流

        一个

        bc

        b

        c

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

        【讨论】:

        • 它似乎不起作用。 ac 不是 abc 的子字符串。
        【解决方案5】:

        试试这样的

        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
            }
        

        【讨论】:

          【解决方案6】:

          这里有一个易于阅读的解决方案

          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);
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2021-02-02
            • 1970-01-01
            • 2011-02-03
            • 2019-03-29
            • 2021-02-09
            • 1970-01-01
            • 2010-10-28
            • 2015-05-27
            相关资源
            最近更新 更多