Skip to content

Commit 5c830ee

Browse files
authored
Create String Rotation (Booth's Algorithm).cpp
1 parent 537cec4 commit 5c830ee

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Returns the lexicographically least rotation of string
2+
3+
string least_rotation(string s)
4+
{
5+
s += s;
6+
vector<int> f(s.size(), -1);
7+
int k = 0;
8+
for(int j = 1; j < s.size(); j++)
9+
{
10+
char sj = s[j];
11+
int i = f[j - k - 1];
12+
while(i != -1 && sj != s[k + i + 1])
13+
{
14+
if(sj < s[k + i + 1])
15+
k = j - i - 1;
16+
i = f[i];
17+
}
18+
if(sj != s[k + i + 1])
19+
{
20+
if(sj < s[k])
21+
k = j;
22+
f[j - k] = -1;
23+
}
24+
else
25+
f[j - k] = i + 1;
26+
}
27+
return s.substr(k, s.size() / 2);
28+
}
29+
30+
//Problem 1: https://www.codechef.com/BIT32020/problems/BIT3B

0 commit comments

Comments
 (0)