Problem 3: ruby rewrite
This commit is contained in:
parent
a70c532a3e
commit
a053af02d9
6 changed files with 70 additions and 90 deletions
|
@ -1 +0,0 @@
|
|||
../template/Makefile
|
|
@ -1,59 +0,0 @@
|
|||
#include <string.h>
|
||||
#include "lib.h"
|
||||
|
||||
void resetArray(int* count) {
|
||||
int length = '~' - 32 + 1;
|
||||
for (int i=0; i<length; i++) {
|
||||
count[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int lengthOfLongestSubstring(char * s) {
|
||||
// Create an array to count every symbol
|
||||
// '~' is one of the last symbols in ascii table (except DEL)
|
||||
int count['~' - 32 + 1];
|
||||
resetArray(count);
|
||||
|
||||
int len = strlen(s);
|
||||
// Start of the current sequence
|
||||
int start = 0;
|
||||
// Result will be stored here
|
||||
int maxlen = 0;
|
||||
|
||||
while (start < len) {
|
||||
// End of the current sequence
|
||||
int end = start;
|
||||
|
||||
while (end < len) {
|
||||
// Calculate index in array
|
||||
int ch = s[end] - 32;
|
||||
// Symbol repeated
|
||||
if (count[ch] == 1) {
|
||||
// Fill array with 0
|
||||
resetArray(count);
|
||||
// Save max length
|
||||
int newmaxlen = end - start;
|
||||
if (newmaxlen > 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;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
int lengthOfLongestSubstring(char * s);
|
|
@ -1,27 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
44
problems/3-longest-substring/main.rb
Normal file
44
problems/3-longest-substring/main.rb
Normal file
|
@ -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
|
26
problems/3-longest-substring/test.rb
Normal file
26
problems/3-longest-substring/test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue