On Hackerrank I came across this problem. I have a function that takes in 3 arguments. Example -->
func getShiftedString(s: String, leftShifts: Int, rightShifts: Int) -> String
Left Shift: A single circular rotation of the string in which the first character becomes the last character and all other characters are shifted one index to the left. For example, abcde becomes bcdea after one left shift and cdeab after two left shifts.
Right Shift A single circular rotation of the string in which the last character becomes the first character and all other characters are shifted to the right. For example, abcde becomes eabcd after one right shift and deabc after two right shifts.
I have done the same question in python and passed all the test case
def getShiftedString(s, leftShifts, rightShifts):
i=(leftShifts-rightShifts)%len(s)
return s[i:]+s[:i]
and now I'm trying to solve the same question in swift. I have added my solution below
func getShiftedString(s: String, leftShifts: Int, rightShifts: Int) -> String {
// Write your code here
let len = s.count
var i=(leftShifts-rightShifts)%len
let c = s[..<i]
let b = s[i...]
let d = c + b
return d }
I'm newer to swift programming I'm not able to solve this question. can anyone help me with how to solve this in swift?
Thanks in advance.