2017-11-04 8 views
3

iOS 11、Swift 4.0文字列のすべての組み合わせを表示する

可能なすべての文字列の組み合わせを表示するための再帰関数を作成しようとしています。私はこれを持っていますが、私はわずか20ペアしか得られないので、それはまあまあです。私は24を得るべきです。私がここで逃したものを見ることはできません。

このコーディングはどこが間違っていますか?

var ans:Set<String>! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let str = "ABCD" 
    ans = [] 
    recursiveString(s2s: str, char2s: 0) 
    print("\(ans) \(ans.count)") 
} 

func recursiveSwap(s2x: String, c2x: Int, j2m: Int) { 
    var anschr = Array(s2x) 
     let tmpchr = anschr[c2x] 
     anschr[c2x] = anschr[c2x+j2m] 
     anschr[c2x+j2m] = tmpchr 
     print("\(String(anschr))") 
      ans.insert(String(anschr)) 
     if (c2x + j2m + 1) < s2x.count { 
      recursiveSwap(s2x: String(s2x), c2x: c2x, j2m: j2m+1) 
     } else { 
      if (c2x + 1) < s2x.count - 1 { 
       recursiveSwap(s2x: String(anschr), c2x: c2x + 1, j2m: 1) 
      } 
     } 
} 

func recursiveString(s2s: String, char2s: Int) { 
    let blue = shiftString(s2s: s2s) 
    if char2s < s2s.count { 
     recursiveSwap(s2x: blue, c2x: 0, j2m: 1) 
     recursiveString(s2s: blue, char2s: char2s + 1) 
    } 
} 

func shiftString(s2s: String) -> String { 
    let str2s = Array(s2s) 
    let newS = str2s.suffix(str2s.count - 1) + str2s.prefix(1) 
    return String(newS) 
} 

それは私を与える...

CBDA DCBA ACDB ADCB ABDC ABCD DCAB ADCB BDAC BADC BCAD BCDA ADBC BADC CABD CBAD CDBA CDAB BACD CBAD DBCA DCBA DACB DABC

+0

を参照してください:https://stackoverflow.com/a/33021952/1630618 – vacawama

+1

「なぜデバッグの助けを求める質問には、特定の問題やエラー、そして質問自体にそれを再現するのに必要な最短のコードが含まれていなければならない」という理由で、 - この質問には*すべて*存在します。 –

+0

https://stackoverflow.com/questions/34968470/calculate-all-permutations-of-a-string-in-swiftの重複? – matt

答えて

3

ないあなたの質問に直接答えが、あなたが得ることができるすべてのpermutations(スウィフトにJavaから翻訳された)次のように:

extension String { 
    private static var permutations: Set<String> = [] 
    var permutations: Set<String> { 
     guard !isEmpty else { return [] } 
     permutate(String(), self) 
     return String.permutations 
    } 
    private func permutate(_ result: String , _ string: String) { 
     if !string.isEmpty { 
      string.lazy.indices.forEach { 
       permutate(result + String(string[$0]), string[..<$0] + String(string[string.index(after: $0)...])) 
      } 
     } else { 
      if String.permutations.insert(result).inserted { 
       print(result) 
      } 
     } 
    } 
} 

let str = "ABCD" 
print(str.permutations.sorted()) // "["ABCD", "ABDC", "ACBD", "ACDB", "ADBC", "ADCB", "BACD", "BADC", "BCAD", "BCDA", "BDAC", "BDCA", "CABD", "CADB", "CBAD", "CBDA", "CDAB", "CDBA", "DABC", "DACB", "DBAC", "DBCA", "DCAB", "DCBA"]\n" 
0

あなたが言ったので:Trying to write a recursive function to show all the possible combinations of a string

は、私が思うあなたのことができるように、このような何か:

// Takes any collection of T and returns an array of permutations 
func permute<C: Collection>(items: C) -> [[C.Iterator.Element]] { 
    var scratch = Array(items) // This is a scratch space for Heap's algorithm 
    var result: [[C.Iterator.Element]] = [] // This will accumulate our result 

    // Heap's algorithm 
    func heap(_ n: Int) { 
     if n == 1 { 
      result.append(scratch) 
      return 
     } 

     for i in 0..<n-1 { 
      heap(n-1) 
      let j = (n%2 == 1) ? 0 : i 
      scratch.swapAt(j, n-1) 
     } 
     heap(n-1) 
    } 

    // Let's get started 
    heap(scratch.count) 

    // And return the result we built up 
    return result 
} 

// We could make an overload for permute() that handles strings if we wanted 
// But it's often good to be very explicit with strings, and make it clear 
// that we're permuting Characters rather than something else. 

let string = "ABCD" 
let perms = permute(string.characters) // Get the character permutations 
let permStrings = perms.map() { String($0) } // Turn them back into strings 
print(permStrings) // output if you like 

私をたくさん助けたこの回答より抜粋一度:Calculate all permutations of a string in Swift

これは多くの順列を与えるでしょう。

私はこれがあなたを助けてくれることを願っています。

+0

ガボックス、これに感謝のおかげで!私はあなたの答えにも目を向けるだろうが、それは私に1つを選択させる。それらは私が思いついたより良いコードです。 – user3069232

+0

これはhttps://stackoverflow.com/a/34969388/1187415のコードと似ていませんが、逐語的なコピー*です。 –

関連する問題