Created
April 5, 2013 20:22
-
-
Save alexandru-calinoiu/5322328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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