Rotate a string left
Rotate a non-empty string left by a given number of positions, allowing positions larger than the string length.
Course progress
Problem 64 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
text = "BuildQuill"; positions = 5
Expected output
QuillBuild
What is happening?
Split after the first five characters, Build. Moving that prefix behind the suffix Quill produces QuillBuild.
Solution strategy
Build the right mental model
Normalize the rotation with modulo, then concatenate the suffix after the split with the prefix before it.
Core concepts
strings · modulo
Complexity
Time O(n), space O(n).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for rotate-string-left
using System;
class Program
{
static string RotateLeft(string text, int positions)
{
if (text.Length == 0) return text;
positions = ((positions % text.Length) + text.Length) % text.Length;
return text.Substring(positions) + text.Substring(0, positions);
}
static void Main()
{
Console.WriteLine(RotateLeft("BuildQuill", 5));
}
}Knowledge check
Questions students often ask
What is the main idea behind rotate a string left?
Normalize the rotation with modulo, then concatenate the suffix after the split with the prefix before it.
What is the time and space complexity of this solution?
Time O(n), space O(n).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.