The problem statement:
"Longest Substring Without Repeating Characters"
Input: "hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expected output: 55
Actual Output: 312
#include <stdio.h>
int lengthOfLongestSubstring(char *s) {
int i = 0;
char arr[255];
int max = 0;
memset(arr, -1, 255);
int si = 0; //substring start index
while (s[i] != '\0') {
if (arr[s[i]] != -1) {
int psi = si; //previous substring start
int pi = arr[s[i]]; //previous index of s[i]
si = pi + 1; //new substring starts
int psl = i - psi; //previous substring length
max = max > psl ? max : psl;
for(int j = psi; j <= pi; j++) {
arr[s[j]] = -1;
}
}
arr[s[i]] = i;
i++;
}
max = max > (i - si) ? max : (i - si);
return max;
}
int main(int argc, char **argv) {
//printf("%s\n", argv[1]);
printf("%d\n", lengthOfLongestSubstring(argv[1]));
return 0;
}