简体   繁体   English

从给定字符串中查找长度为k的所有排列/组合

[英]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. 给定一个字符串,我必须编写程序来查找长度为k的所有排列/组合。 So for string = "cra" and length = 2 Following need to be returned in a vector: "ca","cr","rc","ra","ac","ar". 所以对于string =“cra”和length = 2以下需要在向量中返回:“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. 正如Slava所提到的,你可以使用std::next_permutation但我有一种感觉,面试官希望看到你的技术能力,以了解perms和comps的工作原理。

Here is a useful link. 是一个有用的链接。 It uses Java/C# , I looked at it and it looks like it can be easily converted to C++. 它使用Java / C# ,我看了它,看起来它可以很容易地转换为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: “cra”中大小为k的子字符串是:

  • "c", followed by substrings of size k-1 in "ra" “c”,后跟“ra”中大小为k-1的子串
  • "r", followed by substrings of size k-1 is "ca" “r”,后跟大小为k-1的子串是“ca”
  • "a", followed by substrings of size k-1 in "cr" “a”,然后是“cr”中大小为k-1的子串

So If I write E the sets of n characters, and e_i its elements. 所以如果我写E组的n个字符,e_i它的元素。

  • Substring(E, k) = {""} if k = 0 (yes, I also like initializing recurrences at 0) 子串(E,k)= {“”}如果k = 0(是的,我也喜欢将重现初始化为0)
  • and Substring(E, k) = Union (Substring(e_i + Substring(E\\e_i, k-1))) if k>0 和子串(E,k)= Union(子串(e_i +子串(E \\ e_i,k-1)))如果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. 大小为k的集合E的子串是e_i上的第一个字母为e_i的子串的E的每个元素的并集。

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). 之后,如果存储中间结果,可以通过交换内存使用的计算时间来优化方法,这样您就不必多次计算它们(对于n = 3无关紧要,但它绝对重要当n变大时)。 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. 如果您的起始单词是abcdefgh并且k = 5,那么您将存储子字符串(“cdefgh”,3)之类的内容,这样您就不必为以a开头的单词和以b开头的单词计算它。 You'll save a lot of computation time, but might require a lot of memory when n gets big. 你会节省大量的计算时间,但是当n变大时可能需要大量的内存。 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). 如果你有一个内存阈值,最好存储最小k的子串,就像那些最需要请求的那些(递归树的结尾)。

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. 我选择使用该对的映射(“cdefgh”,3),甚至单独使用“cdefgh”作为键,将子串的集合作为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 n 个字符的长度为 k 的所有排列,在 CPP 中重复 - All permutations of length k from n characters with repetition in CPP 给定一个字符串,找到它在字典中的单词的所有排列 - Given a string, find all its permutations that are a word in dictionary 从给定的字符串中找到给定长度的子序列? - Find subsequence of given length from a given string? 如何从长度为 n 的字符串中获取长度 k 的所有唯一排列的数量? - How to get number of all unique permutations of lengh k out of string of length n? 在给定数组中找到长度为k的所有连续子数组的和 - To find sum of all consecutive sub-array of length k in a given array 给定长度的英文字母的排列 - Permutations of English Alphabet of a given length 如何从一组 k 个元素中生成长度为 n 的所有排列 - How to generate all permutations of lenght n from a set of k elements 如何找到字节的排列或组合? - How do I find permutations or combinations for byte? 对于给定的整数a,找到总和为a的所有唯一正整数组合 - For a given integer a, find all unique combinations of positive integers that sum up to a 使用并行线程查找加起来为给定数字的所有组合 - Find all combinations that add upto given number using parallel threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM