My Swift Simple Solution


  • 0
    Y
    func reverseStr(_ s: String, _ k: Int) -> String {
            var stringArray = s.characters.map({ String($0) })
            var ans:String = ""
            var i = 0
            if k <= 0 || k>1000 {return "Length of the given string and k will in the range [1, 10000]"}
            
            while i < stringArray.count{
                var j = 0
                var temp:[String] = []
                while j < k && i < stringArray.count {  //reverse
                    temp.append(stringArray[i])
                    j += 1
                    i += 1
                }
                ans.append(temp.reversed().joined(separator: ""))
    
                while j < 2*k && i < stringArray.count{  //same
                    ans.append(stringArray[i])
                    i += 1
                    j += 1
                }
            }
            return k==1 ? s : ans
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.