Rotate String Right
Programming Interview
Medium
4 views
Problem Description
For each testcase you get string s and integer k. Rotate string to the right by k and output the new string.
Input Format
First line t. For each: line1 s, line2 k.
Output Format
t lines rotated strings.
Sample Test Case
Input:
2
abcdef
2
hello
7
Constraints
Total length of strings
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines:
sys.exit(0)
try:
t=int(lines[0].strip())
except Exception:
t=0
idx=1
out=[]
for _ in range(t):
s=lines[idx] if idx<len(lines) else ''
idx+=1
try:
k=int((lines[idx] if idx<len(lines) else '0').strip())
except Exception:
k=0
idx+=1
n=len(s)
if n==0:
out.append('')
else:
k%=n
out.append(s[-k:]+s[:-k] if k else s)
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!