Home > Software design >  Reverse string without reversing custom characters swift
Reverse string without reversing custom characters swift

Time:06-14

Need to reverse strings without affecting characters wich user can add by him self. Can somebody rewie my code and suggest the solution ?

func reverseWithoutFilter(FullText:String, TextToIgnore:String) -> String {
    let fullTextArray = FullText.components(separatedBy: " ")
    let textToIgnoreArray = TextToIgnore.components(separatedBy: " ")
    let result = fullTextArray.map{!textToIgnoreArray.contains($0) ? String($0.reversed()) : $0}
    return result.joined(separator: " ")
}
 
var result = reverseWithoutFilter(FullText: "FOX is good", TextToIgnore: "FOX")

// result will be "FOX si doog"

var result1 = reverseWithoutFilter(FullText: "Fox is good", TextToIgnore: "Fx")

// result will be "xoF si doog " characters "F" and "x" are not ignored 

CodePudding user response:

You should

  • extract components in fullText, as you did
  • in each, get the the positions of each char in textToIgnore
  • remove those chars in each component
  • reverse each component
  • insert back the removed chars in the reverted components

Here is a code for this. Can be optimized for sure, but it works.

extension StringProtocol {
    subscript(offset: Int) -> Character {
        self[index(startIndex, offsetBy: offset)]
    }
}

func reverseWithoutFilter2(fullText: String, textToIgnore: String) -> String {
    
    // For example: "Alloha 123", excluding "h3"
    let fullTextArray = fullText.components(separatedBy: " ")
    // fullTextArray = ["Alloha", "123"]
    var posOfChars : [[String:[Int]]] = [] // posOfChars[i] is a dict [String:[Int]], giving the positions of each excluded chars in component i
    // keep positions of excluded chars
    for (i, comp) in fullTextArray.enumerated() {
        posOfChars.append( [:] )
        for c in textToIgnore {
            for (pos, charInComp) in comp.enumerated() {
                if charInComp == c {
                    if posOfChars[i][String(c)] == nil {
                        posOfChars[i][String(c)] = [pos]
                    } else {
                        posOfChars[i][String(c)]!.append(pos)
                    }
                }
            }
        }
    }
    // posOfChars[0]["h"] = [4]
    // posOfChars[1]["3"] = [2]

    var newTextArray = fullTextArray
    // Remove excluded chars in each comp
    for (i, comp) in fullTextArray.enumerated() {
        for c in textToIgnore {
            newTextArray[i] = newTextArray[i].replacingOccurrences(of: String(c), with: "")
        }
    }
    // newTextArray = ["Alloa", "12"]
    
    // Now we reverse each component
    newTextArray = newTextArray.map() { String($0.reversed()) }
    // newTextArray = ["aollA", "21"]
    
    // Put back the excluded chars
    for i in 0..<newTextArray.count {
        for toRestore in posOfChars[i] { // ["h": [4] ]
            let str = toRestore.key // "h" but need to get a char
            let firstChar = str[0] 
            for pos in toRestore.value {
                let posIndex = newTextArray[i].index(newTextArray[i].startIndex, offsetBy: pos)
                newTextArray[i].insert(firstChar, at: posIndex)
            }
        }
    }

    return newTextArray.joined(separator: " ")
}
  • Related