substrings

E

Ed Redman

I am struggling with the following.
Given a sting of a given length example string = 'bkerhcno'
give all the subsets of string of length 4

I don't need all the permutations just the subsets.
I can use the slice method to give me some subsets i.e
s1 = string.slice(0,4)
s2 = string.slice(1,4) etc.
Just how do I generate all the possible combination of 4 characters.

By analagy, would like to extend to finding all combination of length 5,6,
and 7.

Hope someone can help.
 
R

Robert Klemme

Ed said:
I am struggling with the following.
Given a sting of a given length example string = 'bkerhcno'
give all the subsets of string of length 4

I don't need all the permutations just the subsets.
I can use the slice method to give me some subsets i.e
s1 = string.slice(0,4)
s2 = string.slice(1,4) etc.
Just how do I generate all the possible combination of 4 characters.

By analagy, would like to extend to finding all combination of length
5,6, and 7.

Hope someone can help.

def sub_strings(s,len)
(s.length - len).times do |idx|
yield s[idx,len]
end
end
"bker"
"kerh"
"erhc"
"rhcn"
"hcno"
=> 0..4

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top