Suffix Rotation

Problem Statement :

Megan is playing a string game with the following rules:

It starts with a string, s.
During each turn, she performs the following move:

Choose an index in s. The chosen index must be strictly greater than any index chosen in a prior move.
Perform one or more circular rotations (in either direction) of the suffix starting at the chosen index.
For example, let's say s = abcdefjghi. During our move, we choose to do three right rotations of the suffix starting at index 6:
Note that this counts as one move.

The goal of the game is to convert s into the lexicographically smallest possible string in as few moves as possible. In other words, we want the characters to be in alphabetical order.
Megan plays this game g times, starting with a new string s each time. For each game, find the minimum number of moves necessary to convert s into the lexicographically smallest string and print that number on a new line.

Input Format

The first line contains an integer, g, denoting the number of games.
Each of the g subsequent lines contains a single string denoting the initial value of string s for a game.

1 <= g <= 100
1 <= |s| <= 1000

s consists of lowercase English alphabetic letters only.
Output Format

For each game, print an integer on a new line denoting the minimum number of moves required to convert s into the lexicographically smallest string possible.

Solution :


                            Solution in C :

In C++ :

#include <bits/stdc++.h>
using namespace std;

#define N 1010
#define inf 1010

int t;
char s[N];
map <string, int> mp;

int solve(char *s) {
//	puts(s);
	int len = strlen(s);
	if (!len) return 0;
	if (mp.count(s)) return mp[s];
	char c = *min_element(s, s + len);
	if (s[0] == c) return mp[s] = solve(s + 1);
	int rlt = 0;
	char ss[N];
	int runs = 0;
	bool vis[N];
	for (int i = 0; i < len; i ++) if (s[i] != c) {
		ss[runs] = s[i];
		int prv = i ? i - 1 : len - 1;
		vis[runs++] = s[prv] == c;
		if (i < len - 1 && s[i+1] == c) rlt ++;
	ss[runs] = 0;
	len = runs;
	c = *min_element(ss, ss + len);
	bool start_c = 0;
	for (int i = 0; i < len; i ++) {
		if (vis[i] && ss[i] == c) {
			start_c = 1; break;
	int mn = inf;
    char sss[N];
	for (int i = 0; i < len; i ++) if (vis[i]) {
		if (start_c && ss[i] != c) continue;
		for (int j = 0; j < len; j ++) sss[j] = ss[(j+i)%len];
		sss[len] = 0;
		mn = min(mn, solve(sss));
	return mp[s] = rlt + mn;

int main() {
//	freopen("", "r", stdin);
//	freopen("s.out", "w", stdout);
	scanf("%d", &t);
	while (t --) {
		scanf("%s", s);
		printf("%d\n", solve(s));
	return 0;

In Java :

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    static class SH {
        int id=0;
        int[] cnt;
        String cs;
        SH(String s,int[] ant) {
        void prep() {
            StringBuilder sb=new StringBuilder();
            while(id<cs.length()) {
                char mc=getMC();
                if(mc==cs.charAt(id)) {
                else break;
            for(int i=id;i<chs.length;++i) {
                if(i!=id&&chs[i]==chs[i-1]) {
                } else {
        public int mm(){
            if(hm.containsKey(cs)) return hm.get(cs);
            if(0==cs.length()) return 0;
            char mc=getMC();
            String[] stx=cs.split(""+mc);
            int res=0;
            StringBuilder sb=new StringBuilder();
            for(String x:stx) {
                if(x.length()!=0) {
            if(cs.charAt(cs.length()-1)!=mc) --res;
            String ns="";
            int mt=sb.length();
            int j=0;
            HashSet<String> hs=new HashSet();
            for(String bx:stx) {
                if(j==res) break;
                if(bx.length()==0) continue;
                String ts=sb.toString();
                int tmt=cMC(ts,mc);
                if(tmt<mt) {
                } else if(tmt==mt) {
            int rm=Integer.MAX_VALUE;
            for(String bx:hs) {
                int nm=new SH(bx,cnt).mm();
                if(nm<rm) rm=nm;
            return res;
        int cMC(String st,char x) {
            int res=0;
            String[] nxt=st.split(""+x);
            for(String tt:nxt) {
                if(tt.length()!=0) ++res;
            if(st.charAt(st.length()-1)!=x) --res;
            if(st.charAt(0)==x) --res;
            return res;
        char getMC() {
            for(char x='a';x<='z';++x) {
                if(cnt[x-'a']!=0) return x;
            return '*';
    static HashMap<String,Integer> hm=new HashMap();
    public static void main(String[] args) {
        Scanner in = new Scanner(;
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s =;
            int[] cnt=new int[26];
            for(char x:s.toCharArray()) ++cnt[x-'a'];
            // your code goes here
            SH t=new SH(s,cnt);

In C :

#define FOR(a,b,c) for(int a=(b),_for=(c);a<_for;++a)
#define M 1000
#define Z 26
char c[M+5];
int n, P[M+5], A[M+5], B[M+5], C[Z+5], D[M+5], E[M+5];
int min(int a, int b)
    return a < b ? a : b;

int n;
int p[M+5];
char c[M+5];
int cnt[Z+5];
int dp[M+5];
int nju[M+5];
int nx[M+5];
int isti[M+5];

void solve(){
    scanf ("%s",c);
    FOR(i,0,n) p[i]=Z-1-(c[i]-'a');
    FOR(i,0,Z) cnt[i]=0;
    FOR(i,0,n) cnt[p[i]]++;
    FOR(i,0,n) dp[i]=0;
    FOR(i,0,n) nju[i]=0;
    FOR(i,0,n) nx[i]=0;
    FOR(i,0,n) isti[i]=0;
        if (!cnt[cl]) continue;
        FOR(i,0,n) dp[i]=nju[i];
        int last=-1,prvi=-1,sum=0,b1=n,b2=n;
        FOR(i,0,n) if (p[i]<=cl){
            if (last>=0){
                if (p[last]!=p[i] && p[i]==cl) isti[i]=1,sum++;
            else prvi=i;
//        FOR(i,0,n) printf ("%d -> %d\n",i,nx[i]);
        if (p[last]!=p[prvi] && p[prvi]==cl) isti[prvi]=1,sum++;
        int pb1=-1;
        FOR(i,0,n) if (p[i]==cl && p[nx[i]]!=cl){
//            printf ("%d -> %d\n",i,b2);
            if (b2<b1)
                int temp = b1;
                b1 = b2;
                b2 = temp;
//        printf ("%d %d %d ???\n",sum,b1,pb1);
        if (pb1==-1) sum=-1;
        bool flag=0;
            if (p[i]>cl) continue;
            if (p[i]<cl || !isti[i]){
            if (b1==b2 || b2==n){
        if (flag){
            for (int i=pb1;i>=0 && flag;--i){
                if (isti[i]) nju[i]++,flag=0;
            for (int i=n-1;i>=0 && flag;--i){
                if (isti[i]) nju[i]++,flag=0;
//        FOR(i,0,n) printf ("%d %d %d\n",i,isti[i],nju[i]);
//        printf ("\n");
    printf ("%d\n",nju[0]);

int main(){
    int znj;
    scanf ("%d",&znj);
    while(znj--) solve();
    return 0;

In Python3 :


import sys

def bestlastrotation(s,groups,letters,memos):
    if len(letters) < 3:
        return groups[0]
    mn = letters[0]
    mn2 = letters[1]
    mn3 = letters[2]
    lens = len(s)
    groups2 = []
    for g in groups:
        i = g
        while s[i] == mn:
            i = (i + 1) % lens
        if s[i] == mn2 and s[g-1] != mn2:
    if len(groups2) == 0: return groups[0]
    if len(groups2) == 1: return groups2[0][0]
    for gg in groups2:
        j = gg[1]
        while s[j] == mn2 or s[j] == mn:
            j = (j + 1) % lens
        if s[j] != mn3:
            return gg[0]
    if len(letters) < 4:
        return groups2[0][0]
    groupset = set(x[0] for x in groups2)
    ans = 0
    best = lens
    for g in groupset:
        s2 = (s[g:]+s[:g]).replace(mn,'')
        result = min_rotations(s2,memos)
        if best > result:
            best = result
            ans = g  
    return ans

def min_rotations(s,memos):
    if s in memos:
        return memos[s]
    letters = sorted(set(s))
    mn = min(letters)
    ans = 0
    while mn != max(letters):
        i = 0
        while s[i] == mn:
            i += 1
        if i > 0:
            s = s[i:]
        groups = []
        for i in range(len(s)):
            if s[i] == mn and s[i-1] != mn:
        ans += len(groups)
        if len(groups) == 1:
            g = groups[0]
            s = s[g:] + s[:g]
        if len(groups) > 1:
            g = bestlastrotation(s,groups,letters,memos)
            s = s[g:] + s[:g]
        s = s.replace(mn,'')
        letters = letters[1:]
        mn = min(letters)
    memos[s] = ans
    return ans

q = int(input().strip())
for a0 in range(q):
    s = input().strip()
    # your code goes here

View More Similar Problems

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →