Skip to content

Commit

Permalink
Create String Rotation (Booth's Algorithm).cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishgup1 authored Jun 26, 2020
1 parent 537cec4 commit 5c830ee
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions String Rotation (Booth's Algorithm).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//Returns the lexicographically least rotation of string

string least_rotation(string s)
{
s += s;
vector<int> f(s.size(), -1);
int k = 0;
for(int j = 1; j < s.size(); j++)
{
char sj = s[j];
int i = f[j - k - 1];
while(i != -1 && sj != s[k + i + 1])
{
if(sj < s[k + i + 1])
k = j - i - 1;
i = f[i];
}
if(sj != s[k + i + 1])
{
if(sj < s[k])
k = j;
f[j - k] = -1;
}
else
f[j - k] = i + 1;
}
return s.substr(k, s.size() / 2);
}

//Problem 1: https://www.codechef.com/BIT32020/problems/BIT3B

0 comments on commit 5c830ee

Please sign in to comment.