Have a solution but need to optimize
func reverse(stringWithIgnoreSymbols: String, ignoreSymbols: String) -> String {
let words = stringWithIgnoreSymbols.components(separatedBy: " ")
var wordsPositions: [[String:[Int]]] = []
for (index, word) in words.enumerated() {
wordsPositions.append([:])
for ignoredCharacter in ignoreSymbols {
for (indx, words) in word.enumerated() {
if words == ignoredCharacter {
if wordsPositions[index][String(ignoredCharacter)] == nil {
wordsPositions[index][String(ignoredCharacter)] = [indx]
} else {
wordsPositions[index][String(ignoredCharacter)]!.append(indx)
}
}
}
}
}
var newTextArray = words
for (i, _) in words.enumerated() {
for ignoredCharacter in ignoreSymbols {
newTextArray[i] = newTextArray[i].replacingOccurrences(of: String(ignoredCharacter), with: "")
}
}
newTextArray = newTextArray.map() { String($0.reversed()) }
for i in 0..<words.count {
var orderedArray = [(Int, String)] ()
for toRestore in wordsPositions[i] {
for item in toRestore.value {
orderedArray.append((item, toRestore.key))
}
orderedArray = orderedArray.sorted() { $0.0 < $1.0 }
}
for (index, str) in orderedArray {
let posIndex = newTextArray[i].index(newTextArray[i].startIndex, offsetBy: index)
let char = str[0]
newTextArray[i].insert(char, at: posIndex)
}
}
return newTextArray.joined(separator: " ")
}
private extension StringProtocol {
subscript(offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
}
}
Post
Replies
Boosts
Views
Activity
Second example
final class ExceptionRuleReverseManager {
var exceptionElements = String()
func reverse(string: String) -> String {
let words = string.components(separatedBy: " ")
var result = [String]()
for word in words {
result.append(rearrangeWord(word))
}
return String(result.joined(separator: " "))
}
private func rearrangeWord(_ word: String) -> String {
var arrayOfCharacters = Array(word)
if var firstElementIndex = arrayOfCharacters.indices.first,
var secondElementIndex = arrayOfCharacters.indices.last {
while firstElementIndex < secondElementIndex {
if isException(element: word[firstElementIndex]) {
// If first element is exception - skip it
firstElementIndex += 1
// If second element is exception - skip it
} else if isException(element: word[secondElementIndex]) {
secondElementIndex -= 1
} else {
// If both elements are not exceptions - swap them
arrayOfCharacters.swapAt(firstElementIndex, secondElementIndex)
firstElementIndex += 1
secondElementIndex -= 1
}
}
}
return String(arrayOfCharacters)
}
private func isException(element: String.Element) -> Bool {
return exceptionElements.contains(element)
}
}
extension String {
subscript (index: Int) -> String.Element {
let stringIndex = self.index(self.startIndex, offsetBy: index)
return self[stringIndex]
}
}
var ReverseManager = ExceptionRuleReverseManager()
ReverseManager.exceptionElements = "A2"
var input = "Alloha 234"
var result = ReverseManager.reverse(string: input)
print(result)
// result = Aaholl 243
ReverseManager.exceptionElements = "4B"
input = "Ball4 456"
result = ReverseManager.reverse(string: input)
// result = Blla4 465