diff --git a/problems/3-longest-substring/Makefile b/problems/3-longest-substring/Makefile deleted file mode 120000 index 25eaf27..0000000 --- a/problems/3-longest-substring/Makefile +++ /dev/null @@ -1 +0,0 @@ -../template/Makefile \ No newline at end of file diff --git a/problems/3-longest-substring/lib.c b/problems/3-longest-substring/lib.c deleted file mode 100644 index 07c44e8..0000000 --- a/problems/3-longest-substring/lib.c +++ /dev/null @@ -1,59 +0,0 @@ -#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 deleted file mode 100644 index 2f9b260..0000000 --- a/problems/3-longest-substring/lib.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -int lengthOfLongestSubstring(char * s); diff --git a/problems/3-longest-substring/main.c b/problems/3-longest-substring/main.c deleted file mode 100644 index 9651cc0..0000000 --- a/problems/3-longest-substring/main.c +++ /dev/null @@ -1,27 +0,0 @@ -#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; -} diff --git a/problems/3-longest-substring/main.rb b/problems/3-longest-substring/main.rb new file mode 100644 index 0000000..7193f1e --- /dev/null +++ b/problems/3-longest-substring/main.rb @@ -0,0 +1,44 @@ +# @param {String} s +# @return {Integer} +def length_of_longest_substring(s) + # Create a hash map to save + # the position of every symbol + indexes = Hash.new(-1) + + # Start and end of the current sequence s[i...j] + i = 0 + j = 0 + + # Result will be stored here + maxlen = 0 + + while i < s.length + # While we haven't reached the end of the string + # and haven't seen current character before + while j < s.length && indexes[s[j]] == -1 + # Save character's position + indexes[s[j]] = j + # Move to the next character + j += 1 + end + + # We just reached the end of the string + # and we will never find a longer substring. + return [maxlen, j - i].max if j == s.length + + # It's not the end of the string, + # repeated character found. + # Save max length and set "i" + # to the character right after the first repeated. + maxlen = [maxlen, j - i].max + new_i = indexes[s[j]] + 1 + while i != new_i + indexes[s[i]] = -1 + i += 1 + end + indexes[s[j]] = j + j += 1 + end + + maxlen +end diff --git a/problems/3-longest-substring/test.rb b/problems/3-longest-substring/test.rb new file mode 100644 index 0000000..b66df31 --- /dev/null +++ b/problems/3-longest-substring/test.rb @@ -0,0 +1,26 @@ +require_relative 'main' +require 'test/unit' + +class TestLengthOfLongestSubstring < Test::Unit::TestCase + def test_regular_string + assert_equal(3, length_of_longest_substring('abcabcbb')) + assert_equal(3, length_of_longest_substring('pwwkew')) + assert_equal(2, length_of_longest_substring('abba')) + end + + def test_empty_string + assert_equal(0, length_of_longest_substring('')) + end + + def test_one_character + assert_equal(1, length_of_longest_substring('a')) + end + + def test_repeating_characters + assert_equal(1, length_of_longest_substring('bbbbbb')) + end + + def test_very_long_string + assert_equal(86, length_of_longest_substring('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#&()*+/<=>?@[]^`{|}~ ' * 1000)) + end +end