Created
September 12, 2013 01:03
-
-
Save kevinlynx/6532008 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
#include <assert.h> | |
char find(const char *str) { | |
int cnt = 1; | |
char c = *str++; | |
for ( ; *str; ++str) { | |
c = cnt == 0 ? *str : c; | |
cnt = cnt + (*str == c ? 1 : (cnt - 1 < 0 ? -cnt : -1)); | |
} | |
return cnt > 0 ? c : 0; | |
} | |
int main(int argc, char* argv[]) { | |
assert(find("abcd") == 0); | |
assert(find("aba") == 'a'); | |
assert(find("abba") == 0); | |
assert(find("ababa") == 'a'); | |
assert(find("ababb") == 'b'); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment