Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python3 Program for Queries for Rotation and Kth Character of the given String in Constant Time
In this problem, we need to perform queries on a string efficiently. We'll handle two types of queries: left rotation and character access. We'll explore two approaches - a straightforward string manipulation method and an optimized pointer-based solution.
Problem Statement
Given a string and an array of queries, perform operations based on query types ?
(1, l) Perform left rotation of the string l times
(2, l) Access and print the character at position l (1-indexed)
Example
Let's see how the queries work with a sample input ?
# Input: s = "vdflkmng", queries = [[1, 3], [2, 3], [2, 4], [2, 2]]
s = "vdflkmng"
print("Original string:", s)
# Query 1: [1, 3] - Left rotate 3 times
# "vdflkmng" -> "lkmngvdf"
rotated = s[3:] + s[:3]
print("After 3 left rotations:", rotated)
# Query 2: [2, 3] - Get 3rd character
print("3rd character:", rotated[2])
# Query 3: [2, 4] - Get 4th character
print("4th character:", rotated[3])
# Query 4: [2, 2] - Get 2nd character
print("2nd character:", rotated[1])
Original string: vdflkmng After 3 left rotations: lkmngvdf 3rd character: m 4th character: n 2nd character: k
Approach 1: String Rotation
In this approach, we physically rotate the string by creating new substrings and concatenating them ?
def executeQueries(s, queries):
for query in queries:
if query[0] == 1:
# Left rotate the string by query[1] positions
rotation = query[1] % len(s) # Handle rotations greater than string length
s = s[rotation:] + s[:rotation]
else:
# Print character at position query[1] (1-indexed)
index = query[1] - 1
print(s[index])
# Test with sample data
s = "vdflkmng"
queries = [[1, 3], [2, 3], [2, 4], [2, 2]]
executeQueries(s, queries)
m n k
Time Complexity: O(M × N) where M is the number of queries and N is string length
Space Complexity: O(N) for creating rotated strings
Approach 2: Optimized Pointer Method
Instead of rotating the actual string, we use a pointer to track the effective starting position. This avoids string creation overhead ?
def executeQueriesOptimized(s, queries):
start_ptr = 0 # Points to effective start of string
str_len = len(s)
for query in queries:
if query[0] == 1:
# Update pointer instead of rotating string
start_ptr = (start_ptr + query[1]) % str_len
else:
# Calculate actual index considering rotation
actual_index = (start_ptr + query[1] - 1) % str_len
print(s[actual_index])
# Test with sample data
s = "vdflkmng"
queries = [[1, 3], [2, 3], [2, 4], [2, 2]]
executeQueriesOptimized(s, queries)
m n k
Time Complexity: O(M) where M is the number of queries
Space Complexity: O(1) constant space
How the Optimized Approach Works
The key insight is that we don't need to physically move characters. We maintain a start_ptr that represents where the string logically begins after rotations ?
# Demonstrating the pointer logic
s = "vdflkmng"
start_ptr = 0
print(f"Original: {s}, start_ptr: {start_ptr}")
# After 3 left rotations, start_ptr moves to position 3
start_ptr = (start_ptr + 3) % len(s)
print(f"After rotation: start_ptr = {start_ptr}")
# To get 3rd character of rotated string:
# actual_index = (start_ptr + 3 - 1) % len(s) = (3 + 2) % 8 = 5
actual_index = (start_ptr + 3 - 1) % len(s)
print(f"3rd character at actual index {actual_index}: {s[actual_index]}")
Original: vdflkmng, start_ptr: 0 After rotation: start_ptr = 3 3rd character at actual index 5: m
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| String Rotation | O(M × N) | O(N) | Small strings, few queries |
| Pointer Method | O(M) | O(1) | Large strings, many queries |
Conclusion
The optimized pointer approach provides constant time and space complexity for string rotation queries. It's ideal for applications requiring frequent rotations and character access operations on large strings.
