Rotate String by K
Python
Medium
3 views
Problem Description
A string s and integer k are provided. Rotate string to the right by k positions and output.
Input Format
Line1: s. Line2: k.
Output Format
One line rotated.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
s=lines[0]
k=int(lines[1].strip())
if not s:
sys.stdout.write('')
else:
k%=len(s)
sys.stdout.write(s[-k:]+s[:-k])
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!