im asked to build a function that finds the longest substring with no repeating characters in a given string.
this is gonna be a 2 in 1 question bc i want to now if my approach is correct and if "emptying" a hashmap is possible.i used this code:
int lengthOfLongestSubstring(char * s){
int hist[256] = {0};
int count = 0;
int max_count = 0;
while(*s){
hist[*s - '\0']++;
if (hist[*s - '\0'] > 1 ){
//fill hist with zeros to start counting again
max_count = count - 1;
count = 0;
continue;
}
}
return max_count;
}
this code would work if i can refill the used hashmap with zeros, its possible with a loop but that sound counter productive, is there another way? and is that even a right approach for the question?