From 2117e07915612f6e94dc43ad956395cf8fed03be Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 24 Jun 2024 09:51:01 +0300 Subject: [PATCH] 2024-06-24 v. 5.9.5: added "3. Longest Substring Without Repeating Characters" --- README.md | 7 ++++--- leetcode-ruby.gemspec | 2 +- ...t_substring_without_repeating_characters.rb | 18 ++++++++++++++++++ ...t_substring_without_repeating_characters.rb | 13 +++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 lib/medium/3_longest_substring_without_repeating_characters.rb create mode 100644 test/medium/test_3_longest_substring_without_repeating_characters.rb diff --git a/README.md b/README.md index e5d0cf60..c4a2494a 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). ### Medium -| Name | Link to LeetCode | Link to solution | -|--------------------|--------------------------------------------------------|-------------------------------------------| -| 2. Add Two Numbers | [Link](https://leetcode.com/problems/add-two-numbers/) | [Link](./lib/medium/2_add_two_numbers.rb) | +| Name | Link to LeetCode | Link to solution | +|---------------------------------------------------|---------------------------------------------------------------------------------------|--------------------------------------------------------------------------| +| 2. Add Two Numbers | [Link](https://leetcode.com/problems/add-two-numbers/) | [Link](./lib/medium/2_add_two_numbers.rb) | +| 3. Longest Substring Without Repeating Characters | [Link](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Link](./lib/medium/3_longest_substring_without_repeating_characters.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index e7929796..596803c4 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '5.9.4' + s.version = '5.9.5' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/medium/3_longest_substring_without_repeating_characters.rb b/lib/medium/3_longest_substring_without_repeating_characters.rb new file mode 100644 index 00000000..ee37adbc --- /dev/null +++ b/lib/medium/3_longest_substring_without_repeating_characters.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/longest-substring-without-repeating-characters/ +# @param {String} s +# @return {Integer} +def length_of_longest_substring(s) + last_index = ::Array.new(256, -1) + index = 0 + result = 0 + (0...s.length).each do |i| + c = s[i].ord + index = [index, last_index[c] + 1].max + result = [result, i - index + 1].max + last_index[c] = i + end + + result +end diff --git a/test/medium/test_3_longest_substring_without_repeating_characters.rb b/test/medium/test_3_longest_substring_without_repeating_characters.rb new file mode 100644 index 00000000..b6cc725e --- /dev/null +++ b/test/medium/test_3_longest_substring_without_repeating_characters.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/3_longest_substring_without_repeating_characters' +require 'minitest/autorun' + +class LongestSubstringWithoutRepeatingCharactersTest < ::Minitest::Test + def test_default + assert_equal(3, length_of_longest_substring('abcabcbb')) + assert_equal(1, length_of_longest_substring('bbbbb')) + assert_equal(3, length_of_longest_substring('pwwkew')) + end +end