Last active
December 16, 2015 16:09
-
-
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)
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
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