diff --git a/leetcode/problem_3/sol1.c b/leetcode/problem_3/sol1.c index 2acd0823..3d2efa7d 100644 --- a/leetcode/problem_3/sol1.c +++ b/leetcode/problem_3/sol1.c @@ -1,12 +1,33 @@ -int lengthOfLongestSubstring(char *str) +/** + * \file + * \brief [3. Longest + * substring](https://leetcode.com/problems/longest-substring-without-repeating-characters/) + * optimized solution + * \details Given a string `s`, find the length of the longest substring without + * repeating characters. + */ + +#include +#include +#include +#include + +/** + * @brief Optimized Longest substring function. + * + * @param str NULL terminated alpha-numeric string to search + * @return length of longest substring + * @note no spaces or other non-alphanumeric characters + */ +int lengthOfLongestSubstring(const char *str) { - int n = strlen(str); + size_t n = strlen(str); if (!n) return 0; - int L_len = 1; // lenght of longest substring - int C_len = 1; // lenght of current substring + int L_len = 1; // length of longest substring + int C_len = 1; // length of current substring int P_ind, i; // P_ind for previous index int visited[256]; // visited will keep track of visiting char for the last @@ -33,33 +54,25 @@ int lengthOfLongestSubstring(char *str) L_len = C_len; return L_len; } -/* Brute force */ -int lengthOfLongestSubstring(char *s) -{ - int cur_max = 0, max = 0; - int counter[255]; - int end = 0; - memset(counter, 0, sizeof(int) * 255); - while (end < strlen(s)) - { - if (counter[s[end]] == 0) - { - counter[s[end]]++; - end++; - cur_max++; - } - else - { - char c = s[end]; - memset(counter, 0, 255 * sizeof(int)); - if (cur_max >= max) - max = cur_max; - cur_max = 0; - while (s[end - 1] != c) end--; - } - } - if (cur_max >= max) - max = cur_max; - return max; +/** + * @brief Main function + * @return 0 + */ +int main() +{ + int test1 = lengthOfLongestSubstring("abcabcbb"); + assert(test1 == 3); + printf("Test 1 passed\n"); + int test2 = lengthOfLongestSubstring("bbbbb"); + assert(test2 == 1); + printf("Test 2 passed\n"); + int test3 = lengthOfLongestSubstring("pwwkew"); + assert(test3 == 3); + printf("Test 3 passed\n"); + int test4 = lengthOfLongestSubstring(""); + assert(test4 == 0); + printf("Test 4 passed\n"); + + return 0; } diff --git a/leetcode/problem_3/sol2.c b/leetcode/problem_3/sol2.c new file mode 100644 index 00000000..64f18246 --- /dev/null +++ b/leetcode/problem_3/sol2.c @@ -0,0 +1,72 @@ +/** + * \file + * \brief [3. Longest + * substring](https://leetcode.com/problems/longest-substring-without-repeating-characters/) + * brute force solution + * \details Given a string `s`, find the length of the longest substring without + * repeating characters. + */ + +#include +#include +#include +#include + +/** + * @brief Optimized Longest substring function. + * + * @param str NULL terminated alpha-numeric string to search + * @return length of longest substring + * @note no spaces or other non-alphanumeric characters + */ +int lengthOfLongestSubstring(const char *s) +{ + int cur_max = 0, max = 0; + int counter[255]; + int end = 0; + + memset(counter, 0, sizeof(int) * 255); + while (end < strlen(s)) + { + if (counter[s[end]] == 0) + { + counter[s[end]]++; + end++; + cur_max++; + } + else + { + char c = s[end]; + memset(counter, 0, 255 * sizeof(int)); + if (cur_max >= max) + max = cur_max; + cur_max = 0; + while (s[end - 1] != c) end--; + } + } + if (cur_max >= max) + max = cur_max; + return max; +} + +/** + * @brief Main function + * @return 0 + */ +int main() +{ + int test1 = lengthOfLongestSubstring("abcabcbb"); + assert(test1 == 3); + printf("Test 1 passed\n"); + int test2 = lengthOfLongestSubstring("bbbbb"); + assert(test2 == 1); + printf("Test 2 passed\n"); + int test3 = lengthOfLongestSubstring("pwwkew"); + assert(test3 == 3); + printf("Test 3 passed\n"); + int test4 = lengthOfLongestSubstring(""); + assert(test4 == 0); + printf("Test 4 passed\n"); + + return 0; +}