【问题标题】:Sort a string to determine if it is a Anagram or Palindrome in Swift Xcode在 Swift Xcode 中对字符串进行排序以确定它是 Anagram 还是 Palindrome
【发布时间】:2018-01-10 03:55:37
【问题描述】:

我有一个扩展名 String,有两个函数名 isAnagramOf 和 isPalindrome。第一个函数应该将输入作为字符串,然后首先它将用没有空格的空格替换空格,然后对字符串进行排序和比较,并返回一个 Bool 以确定是否是 anagram。 第二个名为 isPalindrome 的函数也将忽略空格和大写,然后它将反转字符串并比较以返回如果它反转。

我是 swift 的新手,并且正在学习教程,但是无论我如何尝试编写它,我都会不断收到这些错误。我现在已经经历了至少 10 次,但无法让它工作

如果有人可以帮助编写这个很棒的代码,我也愿意向向我展示另一种编写方式的人开放。也许先作为一个数组然后对字符串进行排序,但我不确定。

extension String {
  func isAnagramOf(_ s: String) -> Bool {
    let lowerSelf = self.lowercased().replacingOccurrences(of: " ", with: "")
    let lowerOther = s.lowercased().replacingOccurrences(of: " ", with: "")
    return lowerSelf.sorted() == lowerOther.sorted()   // first error:Value of type 'String' has no member 'sorted  
  }

  func isPalindrome() -> Bool {
    let f = self.lowercased().replacingOccurrences(of: " ", with: "")
    let s = String(describing: f.reversed())           //second error:Value of type 'String' has no member 'reversed'
    return f == s
  }
}

【问题讨论】:

    标签: swift string sorting


    【解决方案1】:

    Swift 3String 本身不是一个集合,所以你必须 排序或反转其characters 视图:

    extension String {
        func isAnagramOf(_ s: String) -> Bool {
            let lowerSelf = self.lowercased().replacingOccurrences(of: " ", with: "")
            let lowerOther = s.lowercased().replacingOccurrences(of: " ", with: "")
            return lowerSelf.characters.sorted() == lowerOther.characters.sorted()
        }
    
        func isPalindrome() -> Bool {
            let f = self.lowercased().replacingOccurrences(of: " ", with: "")
            return f == String(f.characters.reversed())
        }
    }
    

    一种更有效的检查回文的方法是

    extension String {
        func isPalindrome() -> Bool {
            let f = self.lowercased().replacingOccurrences(of: " ", with: "")
            return !zip(f.characters, f.characters.reversed()).contains(where: { $0 != $1 })
        }
    }
    

    因为没有创建新的String,并且函数“短路”, 即一旦发现不匹配就返回。

    Swift 4 中,String 是其字符的集合,并且 代码简化为

    extension String {
        func isAnagramOf(_ s: String) -> Bool {
            let lowerSelf = self.lowercased().replacingOccurrences(of: " ", with: "")
            let lowerOther = s.lowercased().replacingOccurrences(of: " ", with: "")
            return lowerSelf.sorted() == lowerOther.sorted()
        }
    
        func isPalindrome() -> Bool {
            let f = self.lowercased().replacingOccurrences(of: " ", with: "")
            return !zip(f, f.reversed()).contains(where: { $0 != $1 })
        }
    }
    

    还要注意

    let f = self.lowercased().replacingOccurrences(of: " ", with: "")
    

    返回一个删除了所有 空格 字符的字符串。如果你想 删除所有空格(空格,制表符,换行符,...),然后使用 例如

    let f = self.lowercased().replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
    

    【讨论】:

    • 谢谢老板..这一切都很棒,帮助很大。我已经把这件事搞砸了一个星期才终于来找你们。
    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2011-03-19
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多