Created
February 11, 2015 08:30
-
-
Save hcccc/a3429bc5d3b291bd44c6 to your computer and use it in GitHub Desktop.
Longest Substring Without Repeating Characters
This file contains hidden or 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
//Longest substring without repeating chars | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int lengthOfLongestSubstring(char *s) { | |
int maxlen = 0; | |
int curlen = 0; | |
int len = strlen(s); | |
int map[256]; | |
memset(map, -1, sizeof(map)); | |
for (int i = 0; i < len; i++) { | |
if (map[s[i]] >= curlen) { | |
maxlen = maxlen > i - curlen ? maxlen : i - curlen; | |
curlen = map[s[i]] + 1; | |
} | |
map[s[i]] = i; | |
} | |
maxlen = maxlen > len - curlen ? maxlen : len - curlen; | |
return maxlen; | |
} | |
int main(void) { | |
char s[] = "abcabcbb"; | |
printf("Length of Longest Substring with non-repeating chars %d\n", lengthOfLongestSubstring(s)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment