Skip to content

Commit

Permalink
Create radostinaTachova.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
radostinaTachova authored Sep 6, 2023
1 parent b34ecf2 commit 56eb553
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//: PERMUTACIONES

import Foundation

//Explicación
//Empezamos con prefijo vacio y en sufijo está la palabra que se quiere permutar.
//Se van recoriendo las letras del sufijo y se van creando todos los posibles prefijos añadiendo letra a letra.
//En el ejemplo: con prefijo letra s, hacemos prefijo + o, prefijo + l que serán los nuevos prefijos, y así hasta que no haya letras en sufijo y hayamos encontrado una permutación.

// PREF || SUF PREF || SUF PREF(¡RESULTADOS!)
//
// s ---->ol ---> so ---> l ----> sol
// ---> sl ---> o ----> slo
//
// o ---->sl ---> os ---> l ----> osl
// ---> ol ---> s ----> ols
//
// l ---->so ---> ls ---> o ----> lso
// ---> lo ---> s ----> los

func perm(pref: String, suf: String, permutations: inout [String] ) {
if suf.count == 0 {
permutations.append(pref)
}
suf.forEach({ letter in
let newPref = pref + String(letter)
var newSuf = suf
if let index = suf.firstIndex(of: letter) {
newSuf = String(suf.prefix(upTo: index) + suf.suffix(from: suf.index(after: index)))
}
perm(pref: newPref, suf: newSuf , permutations: &permutations)
})
}

var myString = "sol"
var permutations: [String] = []
perm(pref: "", suf: myString, permutations: &permutations)

print(permutations)

0 comments on commit 56eb553

Please sign in to comment.