-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0796-RotateString.cs
38 lines (34 loc) · 1.15 KB
/
0796-RotateString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 72ms
// Memory Usage: 21.8 MB
// Link: https://leetcode.com/submissions/detail/344811594/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0796_RotateString
{
public bool RotateString(string A, string B)
{
if (A.Length != B.Length) return false;
if (A.Length < 2) return true;
var shifts = new int[A.Length + 1];
for (int i = 0; i <= A.Length; i++)
shifts[i] = 1;
for (int left = -1, right = 0; right < A.Length; left++, right++)
{
while (left >= 0 && A[left] != A[right])
left -= shifts[left];
shifts[right + 1] = right - left;
}
var index = 0;
foreach (var ch in A + A)
{
while (index >= 0 && B[index] != ch)
index -= shifts[index];
index++;
if (index == A.Length) return true;
}
return false;
}
}
}