You are viewing a single comment's thread. Return to all comments →
Isn't that going to be slow? You're scanning each word up to 26 times.
You can scan it once if you flag each char. And you can even short circuit it in a few spots:
char * twoStrings(char *s1, char *s2) { int s1_letters_present[26] = {0}; for(int letter_count = 0; *s1; s1++) { assert('a' <= *s1 && 'z' >= *s1); if(0 == s1_letters_present[*s1 - 'a']) { // Count number of unique letters so we can exit early // if possible letter_count++; } s1_letters_present[*s1 - 'a'] = 1; if(26 == letter_count) { // All letters are present, therefor a substring in s2 // is guarunteed return "YES"; } } for(; *s2; s2++) { if(s1_letters_present[*s2 - 'a']) { return "YES"; } } return "NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Two Strings
You are viewing a single comment's thread. Return to all comments →
Isn't that going to be slow? You're scanning each word up to 26 times.
You can scan it once if you flag each char. And you can even short circuit it in a few spots: