Swift-旋转字符串判断

题目:给定两个字符串a和b,判断字符串b是否是由a字符串旋转而成.

核心代码:

` func isRotation(orginal:String,rotation:String) -> Bool {

    let len:Int = orginal.characters.count
    
    if len > 0 && len == rotation.characters.count {
        let mergeStr:String = orginal + orginal
        return mergeStr.contains(rotation)
    }
    return false
}

`

测试代码:

var originalStr:String = "FlyElephant" var rotationStr:String = "antFlyEleph" var result:Bool = myString.isRotation(orginal: originalStr, rotation: rotationStr) print("\(rotationStr)是\(originalStr)的结果\(result)")

你可能感兴趣的:(Swift-旋转字符串判断)