Skip to content

2024-06-24 v. 5.9.5: added "3. Longest Substring Without Repeating Characters" #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions lib/medium/3_longest_substring_without_repeating_characters.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading