From b63e4594d9ce7954ad35da42d76aa21e9756cc6d Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Fri, 18 Nov 2022 14:04:58 +0500 Subject: [PATCH] Problem 3: longest substring --- problems/3-longest-substring/Makefile | 1 + problems/3-longest-substring/lib.c | 59 +++++++++++++++++++++++++++ problems/3-longest-substring/lib.h | 3 ++ problems/3-longest-substring/main.c | 27 ++++++++++++ 4 files changed, 90 insertions(+) create mode 120000 problems/3-longest-substring/Makefile create mode 100644 problems/3-longest-substring/lib.c create mode 100644 problems/3-longest-substring/lib.h create mode 100644 problems/3-longest-substring/main.c diff --git a/problems/3-longest-substring/Makefile b/problems/3-longest-substring/Makefile new file mode 120000 index 0000000..25eaf27 --- /dev/null +++ b/problems/3-longest-substring/Makefile @@ -0,0 +1 @@ +../template/Makefile \ No newline at end of file diff --git a/problems/3-longest-substring/lib.c b/problems/3-longest-substring/lib.c new file mode 100644 index 0000000..07c44e8 --- /dev/null +++ b/problems/3-longest-substring/lib.c @@ -0,0 +1,59 @@ +#include +#include "lib.h" + +void resetArray(int* count) { + int length = '~' - 32 + 1; + for (int i=0; i maxlen) + maxlen = newmaxlen; + // Move to the next sequence + start++; + end = start; + } + else { + // Save the symbol + count[ch]++; + // Move to the next symbol + end++; + } + } + // Save max length + int newmaxlen = end - start; + if (newmaxlen > maxlen) + maxlen = newmaxlen; + // Move to the next sequence + start++; + end = start; + } + + return maxlen; +} diff --git a/problems/3-longest-substring/lib.h b/problems/3-longest-substring/lib.h new file mode 100644 index 0000000..2f9b260 --- /dev/null +++ b/problems/3-longest-substring/lib.h @@ -0,0 +1,3 @@ +#pragma once + +int lengthOfLongestSubstring(char * s); diff --git a/problems/3-longest-substring/main.c b/problems/3-longest-substring/main.c new file mode 100644 index 0000000..9651cc0 --- /dev/null +++ b/problems/3-longest-substring/main.c @@ -0,0 +1,27 @@ +#include +#include "lib.h" + +int main() { + printf("Space: %d, z: %d\n", ' ', 'z'); + char* s1 = "abcabcbb"; + int res1 = lengthOfLongestSubstring(s1); + printf("Test 1: %d\n", res1 == 3); + + char* s2 = "bbbbb"; + int res2 = lengthOfLongestSubstring(s2); + printf("Test 2: %d\n", res2 == 1); + + char* s3 = "pwwkew"; + int res3 = lengthOfLongestSubstring(s3); + printf("Test 3: %d\n", res3 == 3); + + char* s4 = ""; + int res4 = lengthOfLongestSubstring(s4); + printf("Test 4: %d\n", res4 == 0); + + char* s5 = "a"; + int res5 = lengthOfLongestSubstring(s5); + printf("Test 5: %d\n", res5 == 1); + + return 0; +}