给定字符串的所有可能排列? [英] All possible permutations of a given String?

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

问题描述

我将如何在 Ruby 中执行此操作?

p "abc".all_possible_permutations

会回来:

<预><代码>["abc","acb","bca","背","cba",出租车",]

编辑

感谢 Jakub Hampl:

class 字符串def all_possible_permutationsself.chars.to_a.permutation.map(&:join)结尾结尾

解决方案

%w[a b c].permutation.map &:join

How would I do this in Ruby?

p "abc".all_possible_permutations

Would return:

[
  "abc",
  "acb",
  "bca",
  "bac",
  "cba",
  "cab",
]

Edit

Thanks to Jakub Hampl:

class String
  def all_possible_permutations
    self.chars.to_a.permutation.map(&:join)
  end
end

解决方案

%w[a b c].permutation.map &:join

这篇关于给定字符串的所有可能排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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