0

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.

2 Answers2

0

A very basic solution, a bit verbose.

func getShiftedString(s: String, leftShifts: Int, rightShifts: Int) -> String {
    
    if s.isEmpty { return s }
    var effectiveLeftShift = leftShifts - rightShifts
    effectiveLeftShift = effectiveLeftShift % s.count // let's avoid multiple circulations
    if effectiveLeftShift == 0 { return s }

    var newS = s
    if effectiveLeftShift > 0 {
        for _ in 0..<effectiveLeftShift {
            let c = newS.first!
            newS = String(newS.dropFirst())
            newS.append((c))
        }
    } else {
        for _ in 0 ..< -effectiveLeftShift {
            let c = newS.last!
            newS = String(newS.dropLast())
            newS = String(c) + newS
        }
    }
    return newS
}

And a little less verbose:

func getShiftedString2(_ s: String, leftShifts: Int, rightShifts: Int) -> String {
    
    if s.isEmpty { return s }
    var effectiveLeftShift = leftShifts - rightShifts
    effectiveLeftShift = effectiveLeftShift % s.count // let's avoid multiple circulations
    if effectiveLeftShift == 0 { return s }
    
    var newS = s
    if effectiveLeftShift > 0 {
        let c = String(s.prefix(effectiveLeftShift))
        newS = String(s.dropFirst(effectiveLeftShift))
        newS.append(c)
    } else {
       let effectiveRightShifts = -effectiveLeftShift
            let c = String(s.suffix(effectiveRightShifts))
            newS = String(s.dropLast(effectiveRightShifts))
            newS = c + newS
    }
    return newS
}
claude31
  • 874
  • 6
  • 8
0

You can create substrings from the ranges.

Code:

func getShiftedString(s: String, leftShifts: Int, rightShifts: Int) -> String {
    var diff = (leftShifts - rightShifts) % s.count
    while diff < 0 {
        diff += s.count
    }
    guard diff != 0 else { return s }

    let diffIndex = s.index(s.startIndex, offsetBy: diff)
    let first = s[diffIndex ..< s.endIndex]
    let last = s[s.startIndex ..< diffIndex]
    return String(first + last)
}
print(getShiftedString(s: "Hello world!", leftShifts: 4, rightShifts: 2))
// Prints: llo world!He

print(getShiftedString(s: "Hello world!", leftShifts: 2, rightShifts: 5))
// Prints: ld!Hello wor
George
  • 25,988
  • 10
  • 79
  • 133