Skip to content

Instantly share code, notes, and snippets.

@hcccc
Created February 11, 2015 08:30
Show Gist options
  • Save hcccc/a3429bc5d3b291bd44c6 to your computer and use it in GitHub Desktop.
Save hcccc/a3429bc5d3b291bd44c6 to your computer and use it in GitHub Desktop.
Longest Substring Without Repeating 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