Given a string S of lowercase English letters, each character is linked to one of T’s unique identifiers. Specifically, character positions are associated with identifiers from 1 to T. For each identifier IDi (ranging from 1 to M), perform a clockwise/right circular shift by 1 on the portion of string S linked to identifier IDi. This operation replaces characters at positions p1, p2, …, px in S (associated with IDi) with characters from the original S at positions px, p1, p2, …, px-1. Return the transformed string S after all operations.
Examples:
Input: N = 13, T = 3, S = “horje“, ID = [1, 2,3, 1, 2, 2, 1, 1, 3, 2, 2, 1, 3] Output: “kesgeskoeferg“ Explanation:
- First, take ID 1, and perform the operation, “keegsfkogeers”.
- Then take ID 2, and perform the operation, “keegeskogfers”.
- Then take ID 3, and perform the operation, “kesgeskoeferg“.
Input: N = 9, T = 4, S = “algorithm”, ID = [1, 4, 2, 2, 3, 4, 1, 2, 4] Output: “tmhgrlaoi” Explanation:
- First, take ID 1, and perform the operation, “tlgoriahm”.
- Then take ID 2, and perform the operation, “tlhgriaom”.
- Then take ID 3, and perform the operation, “tlhgriaom”.
- Then take ID 4, and perform the operation, “tmhgrlaoi“.
Approach: To solve the problem follow the below idea:
- In this approach we organizes the positions of characters with the same identifier using deque data structures, so that, it performs clockwise circular shifts on these position lists, updating their positions to simulate the circular rotation.
- Afterward, it constructs the final result string by mapping characters from the original positions to their newly shifted positions, completing the circular shift process.
- The code’s efficient utilization of deque and careful manipulation of positions ensures that characters are shifted correctly within their respective colours, leading to the desired modified string output.
Steps to implement the above approach:
- Read the values of N (length of the string) and T (number of identifiers). Read the string S from the input.
- Create a vector of deques named positions of size T+1 . Each deque will store positions of characters corresponding to their identifier.
- Loop through the string S and for each character, read its associated identifier. Push the current position into the deque corresponding to that ID.
- Perform circular shifts: For each color from 1 to T, perform the following steps:
- Retrieve the deque of positions for the current ID.
- Initialize shift_amount to 1 (for clockwise shift).
- Create a new deque named shifted_pos as a copy of the original deque.
- Loop through the positions in the deque:
- Update each position in the shifted_pos deque by shifting it right by shift_amount positions (using modulo for circular effect).
- Replace the original deque with the shifted_pos deque.
- Initialize an empty string named result. For each ID from 1 to T, perform the following steps:
- Retrieve the deque of positions for the current ID.
- Loop through the positions in the deque:
- Update the character in the result string at the current position with the character from the original string S at the shifted position.
- Print the result string, which represents the final modified string after circular shifts.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
#define int long long int
using namespace std;
string transformString( int N, int T, string S,
vector< int > ID)
{
vector<deque< int > > positions(T + 1);
for ( int i = 0; i < N; ++i) {
positions[ID[i]].push_back(i);
}
for ( int identifier = 1; identifier <= T;
++identifier) {
deque< int >& pos = positions[identifier];
int shift_amount = 1;
deque< int > shifted_pos = pos;
for ( int i = 0; i < pos.size(); ++i) {
shifted_pos[(i + shift_amount) % pos.size()]
= pos[i];
}
pos = shifted_pos;
}
string result = S;
for ( int identifier = 1; identifier <= T;
++identifier) {
const deque< int >& pos = positions[identifier];
for ( int i = 0; i < pos.size(); ++i) {
result[pos[(i + 1) % pos.size()]] = S[pos[i]];
}
}
return result;
}
signed main()
{
int N = 13, T = 3;
string S = "horje" ;
vector< int > ID
= { 1, 2, 3, 1, 2, 2, 1, 1, 3, 2, 2, 1, 3 };
string transformedString = transformString(N, T, S, ID);
cout << transformedString << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class CircularShiftTransformation {
public static String transformString( int N, int T, String S, int [] ID) {
List<Integer>[] positions = new ArrayList[T + 1 ];
for ( int i = 0 ; i <= T; i++) {
positions[i] = new ArrayList<>();
}
for ( int i = 0 ; i < N; i++) {
positions[ID[i]].add(i);
}
for ( int identifier = 1 ; identifier <= T; identifier++) {
List<Integer> pos = positions[identifier];
int shiftAmount = 1 ;
List<Integer> shiftedPos = new ArrayList<>(pos);
for ( int i = 0 ; i < pos.size(); i++) {
shiftedPos.set((i + shiftAmount) % pos.size(), pos.get(i));
}
positions[identifier] = shiftedPos;
}
char [] result = S.toCharArray();
for ( int identifier = 1 ; identifier <= T; identifier++) {
List<Integer> pos = positions[identifier];
List<Integer> shiftedPos = new ArrayList<>(pos);
for ( int i = 0 ; i < pos.size(); i++) {
result[shiftedPos.get((i + 1 ) % pos.size())] = S.charAt(pos.get(i));
}
}
return new String(result);
}
public static void main(String[] args) {
int N = 13 ;
int T = 3 ;
String S = "horje" ;
int [] ID = { 1 , 2 , 3 , 1 , 2 , 2 , 1 , 1 , 3 , 2 , 2 , 1 , 3 };
String str = transformString(N, T, S, ID);
System.out.println(str);
}
}
|
Python3
def transformString(N, T, S, ID ):
positions = [[] for _ in range (T + 1 )]
for i in range (N):
positions[ ID [i]].append(i)
for identifier in range ( 1 , T + 1 ):
pos = positions[identifier]
shift_amount = 1
shifted_pos = pos[:]
for i in range ( len (pos)):
shifted_pos[(i + shift_amount) % len (pos)] = pos[i]
positions[identifier] = shifted_pos
result = list (S)
for identifier in range ( 1 , T + 1 ):
pos = positions[identifier]
for i in range ( len (pos)):
result[pos[(i + 1 ) % len (pos)]] = S[pos[i]]
return ''.join(result)
N = 13
T = 3
S = "horje"
ID = [ 1 , 2 , 3 , 1 , 2 , 2 , 1 , 1 , 3 , 2 , 2 , 1 , 3 ]
transformedString = transformString(N, T, S, ID )
print (transformedString)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static string TransformString( int N, int T, string S, List< int > ID)
{
List<Queue< int >> positions = new List<Queue< int >>(T + 1);
for ( int i = 0; i <= T; i++)
{
positions.Add( new Queue< int >());
}
for ( int i = 0; i < N; i++)
{
positions[ID[i]].Enqueue(i);
}
for ( int identifier = 1; identifier <= T; identifier++)
{
Queue< int > pos = positions[identifier];
for ( int i = 0; i < pos.Count; i++)
{
pos.Enqueue(pos.Dequeue());
}
}
char [] result = S.ToCharArray();
for ( int identifier = 1; identifier <= T; identifier++)
{
Queue< int > pos = positions[identifier];
for ( int i = 0; i < pos.Count; i++)
{
result[(pos.Peek() + 1) % pos.Count] = S[pos.Dequeue()];
}
}
return new string (result);
}
static void Main()
{
int N = 13;
int T = 3;
string S = "horje" ;
List< int > ID = new List< int > { 1, 2, 3, 1, 2, 2, 1, 1, 3, 2, 2, 1, 3 };
string transformedString = TransformString(N, T, S, ID);
Console.WriteLine(transformedString);
}
}
|
Javascript
function transformString(N, T, S, ID) {
const positions = [];
for (let i = 0; i <= T; i++) {
positions.push([]);
}
for (let i = 0; i < N; ++i) {
positions[ID[i]].push(i);
}
for (let identifier = 1; identifier <= T; ++identifier) {
const pos = positions[identifier];
const shiftAmount = 1;
const shiftedPos = [...pos];
for (let i = 0; i < pos.length; ++i) {
shiftedPos[(i + shiftAmount) % pos.length] = pos[i];
}
positions[identifier] = shiftedPos;
}
const result = S.split( '' );
for (let identifier = 1; identifier <= T; ++identifier) {
const pos = positions[identifier];
const shiftedPos = [...pos];
for (let i = 0; i < pos.length; ++i) {
result[shiftedPos[(i + 1) % pos.length]] = S[pos[i]];
}
}
return result.join( '' );
}
function main() {
const N = 13;
const T = 3;
const S = "horje" ;
const ID = [1, 2, 3, 1, 2, 2, 1, 1, 3, 2, 2, 1, 3];
const str = transformString(N, T, S, ID);
console.log(str);
}
main();
|
Time Complexity: O(T*N), the dominant factor in terms of time complexity is the circular shift operation, which takes O(T*N) time. Auxiliary Space: O(N+T), where N is the length of the input string and T is the number of unique identifiers.
|