Skip to content

Instantly share code, notes, and snippets.

@alexandru-calinoiu
Created April 5, 2013 20:22
Show Gist options
  • Save alexandru-calinoiu/5322328 to your computer and use it in GitHub Desktop.
Save alexandru-calinoiu/5322328 to your computer and use it in GitHub Desktop.
using System.Text;
public class KDoubleSubstrings
{
public int howMuch(string[] str, int k)
{
int result = 0;
StringBuilder stringBuilder = new StringBuilder();
foreach (string s in str)
{
stringBuilder.Append(s);
}
string all = stringBuilder.ToString();
for (int step = 1; step <= all.Length / 2; step++)
{
for (int index = 0; index <= all.Length - (2 * step); index++)
{
int currentK = CalculateK(GetSubstring(index, step, all));
if (currentK <= k)
{
result++;
}
}
}
return result;
}
public string GetSubstring(int index, int step, string all)
{
string firstHalf = all.Substring(index, step);
string secondHalf = all.Substring(index + step, step);
return firstHalf + secondHalf;
}
public int CalculateK(string str)
{
int lenght = (str.Length / 2);
string firstHalf = str.Substring(0, lenght);
string secondHalf = str.Substring(lenght, lenght);
int result = 0;
for (int i = 0; i < lenght; i++)
{
if (firstHalf[i] != secondHalf[i])
{
result++;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment