简体   繁体   中英

Find all permutations/combinations of length k from given string

This was asked to me in an interview. Given a string, I had to write program to find all permutations/combinations of length k. So for string = "cra" and length = 2 Following need to be returned in a vector: "ca","cr","rc","ra","ac","ar". Repetition is not allowed.

Any suggestions how to go about it?

What I came up with was using repetition. Basically looped over all characters and added it to sequence. When length matched to given length, it would append to final vector.

As Slava mentioned, you could use std::next_permutation but I have a feeling the interviewer wanted to see your technical ability to understand how perms and comps work.

Here is a useful link. It uses Java/C# , I looked at it and it looks like it can be easily converted to C++.

The link contains strong commenting, which is perfect to understand the inner workings of the solution.

I hope you can find this useful for your next interviews. :)

I'd look for something recursive, because I like recursion. Substrings of size k in "cra" are:

  • "c", followed by substrings of size k-1 in "ra"
  • "r", followed by substrings of size k-1 is "ca"
  • "a", followed by substrings of size k-1 in "cr"

So If I write E the sets of n characters, and e_i its elements.

  • Substring(E, k) = {""} if k = 0 (yes, I also like initializing recurrences at 0)
  • and Substring(E, k) = Union (Substring(e_i + Substring(E\\e_i, k-1))) if k>0

This kind of things fits better on a black board than in digital text. Let's try plain text:

The substrings of a set E of size k are the union over e_i each element of E of the substrings whose first letter is e_i.

Am I clear? I don't know if I'm clear.

After that, it's possible to optimize the method by trading computation time for memory use, if you store intermediate results, so that you don't have to compute them several times (doesn't matter for n = 3, but it can definitely matter when n gets big). If your starting word is abcdefgh and k = 5, you'll store things like substring("cdefgh", 3), so that you don't have to compute it for both words starting with a and words starting with b. You'll save a lot of computation time, but might require a lot of memory when n gets big. If you have a threshold on memory, better store the substrings for the smallest k, as those as the ones that'll get requested the most (end of recursion tree).

Last question: how to store it? I'd chose a map using the pair ("cdefgh", 3), or even "cdefgh" alone as key, and the set of substring as value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM