ios - 删除字符串中所有两次重复字符的方法

标签 ios swift string character

我写了一个方法来删除字符串中的所有 2 个重复字符,例如
我只需要删除包含两次的字符,例如
"bndkss"-> "bndk"
"nnmmhj"-> "hj"
"aaabbaac"-> "ac
“阿爸”->“”

我写在 objc 上,一切正常,但 Swift 不工作,请帮忙,我哪里错了?

override func viewDidLoad() {
    super.viewDidLoad()

    let string = "baab"
    print("before: \(string)")
    let stringAfter = checkString(string: string)
    print("after: \(stringAfter)")
}

func checkString(string : String) -> String {
    var tempString = string
    for (index, element) in string.characters.enumerated() {

        for (index2, element2) in string.characters.enumerated() {
            if element == element2 && index != index2 {
                if index > index2 {
                    tempString.remove(at: tempString.index(tempString.startIndex, offsetBy: index))
                    tempString.remove(at: tempString.index(tempString.startIndex, offsetBy: index2))
                } else {
                    tempString.remove(at: tempString.index(tempString.startIndex, offsetBy: index2))
                    tempString.remove(at: tempString.index(tempString.startIndex, offsetBy: index))
                }

                if tempString.characters.count < 1 {
                    return ""
                } else {
                    checkString(string: tempString)
                }
            } else {
                if index == tempString.characters.count - 1 && index2 == tempString.characters.count - 1 {
                    return tempString
                }
            }
        }
    }

    return ""
}

更新: 刚需

return checkString(string: tempString)

代替

checkString(string: tempString)

最佳答案

你的代码有两个问题,比如

  • 删除tempString中的字符后,索引indexindex2到long指的是tempString中的原始字符>。 错误的字符因此被删除。
  • 您递归调用 checkString() 但丢弃结果。

更新正如您同时注意到的那样,return checkString(string: tempString) 解决了这些问题。

这是另一种实现方式。这个想法是使用字典 记住最后一次看到一个字符的位置,以及一个索引集 它跟踪要到达的字符的位置 被保存。而不是两个嵌套循环和递归,两个“简单” 这里使用循环,加上字典和集合操作的成本。

func removeDuplicateCharacters(string: String) -> String {

    var seen = [Character: Int]()
    var keep = IndexSet(integersIn: 0..<string.characters.count)

    for (idx, c) in string.characters.enumerated() {
        if let prevIndex = seen[c] {
            keep.remove(prevIndex)
            keep.remove(idx)
            seen.removeValue(forKey: c)
        } else {
            seen[c] = idx
        }
    }
    return String(keep.map { string[string.index(string.startIndex, offsetBy: $0)] })
}

例子:

print(removeDuplicateCharacters(string: "bndkss")) // ""bndk" 
print(removeDuplicateCharacters(string: "nnmmhj")) // "jh"
print(removeDuplicateCharacters(string: "abba"))   // ""
print(removeDuplicateCharacters(string: "aaabbaac")) // "ac"

关于ios - 删除字符串中所有两次重复字符的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330903/

相关文章:

Core Data 中的 Swift 元组

objective-c - 使用命名参数实现委托(delegate)

string - 如何用 Salt 转义复杂的字符串?

javascript - 使用 JavaScript 递归计算字符串中的元音

python - 大字符串的某些部分不会被 str.replace() 函数替换

ios - SQLite iOS 警告 : Comparison of constant 101 with expression of type BOOL is always false

ios - 无法在 iOS 中解密 AES128

ios - iPhone音频资源

swift - 在 Swift 4 中使用reduce 连接数组和条件

iphone - iPad3拆分键盘时出现灰线问题