Given a string, move all digit elements to end of string. While moving elements, keep the relative order of all positioned elements same.
For example, if the given string is
a1b2c3d4e5f6g7h8i9j1k2l3m4
convert it to
abcdefghijklm1234567891234
in-place and in O(n) time complexity.
I got a different result
abcdefghijklm7481951326324
Also I failed for testing another string
aHR0cDovL3d3dy5nZWVrc2ZvcmdlZWtzLm9yZy9hbi1pbi1wbGFjZS1hbGdvcml0aG0tZm9yLXN0cmluZy10cmF
Code:
static string s = "a1b2c3d4e5f6g7h8i9j1k2l3m4";
static void Main(string[] args)
{
char[] input = s.ToCharArray();
string output = arrangeList(input);
Console.WriteLine(output);
Console.WriteLine("Another test");
s = "aHR0cDovL3d3dy5nZWVrc2ZvcmdlZWtzLm9yZy9hbi1pbi1wbGFjZS1hbGdvcml0aG0tZm9yLXN0cmluZy10cmF";
input = s.ToCharArray();
output = arrangeList(input);
Console.WriteLine(output);
Console.Read();
}
private static string arrangeList(char[] x)
{
for (int i = 1; i < x.Length - 1; i++)
{
int j = i + 1;
while (j < x.Length)
{
if ( (x[j] > '0' && x[j] < '9') && x[i] > '9')
{
swap(x, i, j); j++;
break;
}
if ( (x[i] > '0' && x[i] < '9') && x[j] > '9')
{
swap(x, i, j); j++;
break;
}
j++;
}
}
return new string(x);
}
private static void swap(char[] a, int i, int j)
{
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}