fork download
  1. /*Write a program to take a string s as input. The task is to find the length of the longest substring without
  2. repeating characters. Print the length as output.*/
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. int main() {
  7. char s[1000];
  8. int last[256], i, start = 0, maxLen = 0;
  9.  
  10. scanf("%s", s);
  11. memset(last, -1, sizeof(last));
  12.  
  13. for(i = 0; s[i]; i++) {
  14. if(last[(unsigned char)s[i]] >= start)
  15. start = last[(unsigned char)s[i]] + 1;
  16.  
  17. last[(unsigned char)s[i]] = i;
  18.  
  19. if(i - start + 1 > maxLen)
  20. maxLen = i - start + 1;
  21. }
  22.  
  23. printf("%d", maxLen);
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 5328KB
stdin
abcabcbb
stdout
3