I have 2 strings for example:
abcabc and bcabca
or
aaaabb and abaaba
I checked that second string is the same or not like first string but shifted.
bcabca = ..abca + bc...
using System;
public class TOPSES
{
static string t1, t2;
static char[] t1c;
static char[] t2c;
static void Main()
{
int t;
t = int.Parse(Console.ReadLine());
while (t > 0)
{
t1 = Console.ReadLine();
t2 = Console.ReadLine();
string result = "no";
t1c = t1.ToCharArray();
t2c = t2.ToCharArray();
result = ChangeString(t1c[0], 0);
Console.WriteLine(result);
t--;
}
}
public static string ChangeString(char letter, int startIndex)
{
int index = t2.IndexOf(letter, startIndex);
if (index == -1)
return "no";
else
{
string newString = t2.Remove(0, index);
newString += t2.Remove(index, t2.Length - index);
if (t1 != newString)
return ChangeString(letter, ++index);
return "yes";
}
}
}
How can I improve this code to make it faster? Also, it will be nice if someone could explain what makes this 'program' slow.
booltrue/falseinstead of"yes"/"no". You should be able to time the bottleneck yourself. \$\endgroup\$aais a valid cycling ofaabbcc. \$\endgroup\$