Skip to content

Instantly share code, notes, and snippets.

@lakario
Last active December 16, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakario/5461273 to your computer and use it in GitHub Desktop.
Save lakario/5461273 to your computer and use it in GitHub Desktop.
Calculate the greatest distance between two any same characters in a string. Examples: (in: abba | out: 2), (in: abab | out: 1), (in: aaaabababbbbba | out: 5)
int GetDistance(string val) {
int max = -1;
for(var i = 0; i <= val.Length / 2; i++) {
for(var y = val.Length - 1; y > 0; y--) {
var dist = (y - i) - 1;
if(val[i] == val[y] && dist > max) {
max = dist;
break;
}
}
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment