置换一个字符串打印所有可能的话 [英] Permute a string to print all possible words

查看:168
本文介绍了置换一个字符串打印所有可能的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的code似乎是在寻找与运行时间和空间的渐进措施坏 我越来越 T(N)= T(N-1)* N + O((N-1!)* N),其中N是输入的大小。我要奉劝优化它

既然是不使用任何库的基于算法的面试问题,以实现最有效的方式逻辑要求我们

下面是我的code

 高清str_permutations(str_input,我):
    如果len(str_input)== 1:
        返回[str_input]
    comb_list = []
    而I< LEN(str_input):
        键= str_input [I]
        如果我+ 1 = LEN(str_input)!
            remaining_str =。加入((str_input [0:I],str_input [我+ 1:]))
        其他:
            remaining_str = str_input [0:I]
        all_combinations = str_permutations(remaining_str,0)
        对于指数,价值历数(all_combinations):
            all_combinations [指数] =。加入((键,值))
        comb_list.extend(all_combinations)
        I = I + 1
    回报comb_list
 

解决方案

正如我在评论这个问题提到的,在一般情况下,你不会得到低于指数的复杂性,因为对 N 鲜明的人物,也有 N!输入字符串的排列和O(2 N )为邻的一个子集( N!)。

现在下面将不会改善一般情况下的渐进复杂,但可以优化生产的所有排列为有多次出现一些字符的字符串的蛮力的方法。举个例子字符串 daedoid ;如果你一味地产生它的所有排列,你会得到每一个排列 6 = 3!次,因为你有三个事件的 D 。那么就可以避免先消除多次出现相同的信,而是要记得如何经常使用每个字母。所以,如果有一个字母 C 的带有K <子> C 事件,你可以节省氏子> C !排列。因此,在总,这样可以节省你的一个因素的产品用K <子> C !为所有的C。

The code that i have written seems to be looking bad with asymptotic measure of running time and space I am getting T(N) = T(N-1)*N + O((N-1!)*N) where N is the size of input. I need advise to optimize it

Since it is an algorithm based interview question we are required to implement the logic in most efficient way without using any libraries

Here is my code

def str_permutations(str_input,i):
    if len(str_input) == 1:
        return [str_input]
    comb_list = []
    while i < len(str_input):
        key = str_input[i]
        if i+1 != len(str_input):
            remaining_str = "".join((str_input[0:i],str_input[i+1:]))
        else:
            remaining_str = str_input[0:i]
        all_combinations = str_permutations(remaining_str,0)
        for index,value in enumerate(all_combinations):
            all_combinations[index] = "".join((key,value))
        comb_list.extend(all_combinations)
        i = i+1
    return comb_list

解决方案

As I mentioned in a comment to the question, in the general case you won't get below exponential complexity since for n distinct characters, there are n! permutations of the input string, and O(2n) is a subset of O(n!).

Now the following won't improve the asymptotic complexity for the general case, but you can optimize the brute-force approach of producing all permutations for strings that have some characters with multiple occurrences. Take for example the string daedoid; if you blindly produce all permutations of it, you'll get every permutation 6 = 3! times since you have three occurrences of d. You can avoid that by first eliminating multiple occurrences of the same letter and instead remembering how often to use each letter. So if there is a letter c that has kc occurrences, you'll save kc! permutations. So in total, this saves you a factor of "product over kc! for all c".

这篇关于置换一个字符串打印所有可能的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆