Right-rotate a string n times and count the distinct rotations produced

Quick Overview

Apply n right rotations to a string, return the final string, and count how many distinct strings appear along the way, the original included. Tests modular reasoning about rotations and finding a string's smallest repeating period in linear time for long inputs.

Right-rotate a string n times and count the distinct rotations produced

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s` and an integer `n`, perform `n` right rotations on the string. A single right rotation moves the last character of the string to the front. Count the total number of distinct strings generated across all rotations, including the original string before any rotation is applied. Return a list containing the final rotated string and the count of distinct strings as `[rotated_string, count]`. ### Function Signature ```python def right_rotate_string(s: str, n: int) -> list: ``` ### Rules - The strings to count are the ones after 0, 1, 2, ..., `n` right rotations, so `n + 1` strings with the original included. Equal strings count once. - Return a two-element list: the string after all `n` rotations, then the count as an integer. ### Constraints - `1 <= s.length <= 10^5` - `1 <= n <= 10^6` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "abcde", n = 2 Output: ["deabc", 3] ``` Rotation 0 is "abcde", rotation 1 is "eabcd" and rotation 2 is "deabc". All three differ, so the final string is "deabc" and the count is 3. ```text Input: s = "aaa", n = 3 Output: ["aaa", 1] ``` All rotations of "aaa" produce "aaa". The final string is "aaa" and there is only 1 distinct string. ```text Input: s = "abab", n = 5 Output: ["baba", 2] ``` The rotations alternate between "abab" and "baba", so the six strings for rotations 0 through 5 contain only 2 distinct values. Rotation 5 is odd, so the final string is "baba".

Overview: Apply n right rotations to a string, return the final string, and count how many distinct strings appear along the way, the original included. Tests modular reasoning about rotations and finding a string's smallest repeating period in linear time for long inputs.

Salesforce logo
Salesforce
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s and an integer n, perform n right rotations on the string. A single right rotation moves the last character of the string to the front.

Count the total number of distinct strings generated across all rotations, including the original string before any rotation is applied.

Return a list containing the final rotated string and the count of distinct strings as [rotated_string, count].

Function Signature

def right_rotate_string(s: str, n: int) -> list:

Rules

  • The strings to count are the ones after 0, 1, 2, ..., n right rotations, so n + 1 strings with the original included. Equal strings count once.
  • Return a two-element list: the string after all n rotations, then the count as an integer.

Constraints

  • 1 <= s.length <= 10^5
  • 1 <= n <= 10^6
  • s consists of lowercase English letters.

Examples

Input: s = "abcde", n = 2
Output: ["deabc", 3]

Rotation 0 is "abcde", rotation 1 is "eabcd" and rotation 2 is "deabc". All three differ, so the final string is "deabc" and the count is 3.

Input: s = "aaa", n = 3
Output: ["aaa", 1]

All rotations of "aaa" produce "aaa". The final string is "aaa" and there is only 1 distinct string.

Input: s = "abab", n = 5
Output: ["baba", 2]

The rotations alternate between "abab" and "baba", so the six strings for rotations 0 through 5 contain only 2 distinct values. Rotation 5 is odd, so the final string is "baba".