Note that there are some explanatory texts on larger screens.

plurals
  1. POSubsequence of a string
    text
    copied!<p>I have to write a program that takes string argument s and integer argument k and prints out all subsequences of s of length k. For example if I have </p> <pre><code>subSequence("abcd", 3); </code></pre> <p>the output should be</p> <pre><code> abc abd acd bcd </code></pre> <p>I would like guidance. No code, please!</p> <p>Thanks in advance.</p> <p>Update:</p> <p>I was thinking to use this pseudocode:</p> <pre><code>Start with an empty string Append the first letter to the string Append the second letter Append the third letter Print the so-far build substring - base case Return the second letter Append the fourth letter Print the substring - base case Return the first letter Append the third letter Append the fourth letter Print the substring - base case Return third letter Append the second letter Append the third letter Append the fourth letter Print the substring - base case Return the third letter Return the second letter Append the third letter Append the fourth letter Return third letter Return fourth letter Return third letter Return second letter Return first letter </code></pre> <p>The different indent means going deeper in the recursive calls.</p> <p>(In response to Diego Sevilla):</p> <p>Following your suggestion: </p> <pre><code>private String SSet = ""; private String subSequence(String s, int substr_length){ if(k == 0){ return SSet; } else{ for(int i = 0; i &lt; substr_length; i++){ subString += s.charAt(i); subSequence(s.substring(i+1), k-1); } } return SSet; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload