java - Subsequence of a string -
java - Subsequence of a string -
i have write programme takes string argument s , integer argument k , prints out subsequences of s of length k. illustration if have
subsequence("abcd", 3);
the output should be
abc abd acd bcd
i guidance. no code, please!
thanks in advance.
update:
i thinking utilize pseudocode:
start empty string append first letter string append sec letter append 3rd letter print so-far build substring - base of operations case homecoming sec letter append 4th letter print substring - base of operations case homecoming first letter append 3rd letter append 4th letter print substring - base of operations case homecoming 3rd letter append sec letter append 3rd letter append 4th letter print substring - base of operations case homecoming 3rd letter homecoming sec letter append 3rd letter append 4th letter homecoming 3rd letter homecoming 4th letter homecoming 3rd letter homecoming sec letter homecoming first letter
the different indent means going deeper in recursive calls.
(in response diego sevilla):
following suggestion:
private string sset = ""; private string subsequence(string s, int substr_length){ if(k == 0){ homecoming sset; } else{ for(int = 0; < substr_length; i++){ substring += s.charat(i); subsequence(s.substring(i+1), k-1); } } homecoming sset; } }
as include "recursion" tag, i'll seek explain strategy solution. recursive function should function show:
subsequence(string, substr_length)
that returns set
of (sub)-strings. note how problem divided in sub-problems apt recursion. each subsequence(string, substr_length) should:
subsequence(string[i+1..length], substr_length - 1)
(here ..
imply index range string, have create substring using these indices). recursive phone call subsequence
homecoming strings of size substr_length -1
. have prepend substrings character selected (in case string[i]), , add together of them sset set. just homecoming constructed sset. 1 contain substrings. of course, process highly optimizable (for illustration using dynamic programming storing substrings of length i), idea.
java recursion
Comments
Post a Comment