Skip to content

Commit f6d2492

Browse files
committed
longest
1 parent ca976a5 commit f6d2492

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

Longest Substring Without Repeating Characters /Solution1.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def length_of_longest_substring(s)
1818
max=0
1919
s.each_char.with_index do |c ,idx|
2020
if !hash.has_key?(c)
21-
puts "#{c}: Does not exist, push #{c} to #{idx}"
21+
#puts "#{c}: Does not exist, push #{c} to #{idx}"
2222
hash[c]=idx
2323
else
2424
if idx-hash[c] > max
2525
max = idx-hash[c]
2626
end
27-
puts "The maximum distance of #{c} is #{idx-hash[c]}"
28-
puts "delete from #{last_delete} to #{hash[c]-last_delete+1}, #{s[last_delete, hash[c]-last_delete+1]}"
27+
#puts "The maximum distance of #{c} is #{idx-hash[c]}"
28+
#puts "delete from #{last_delete} to #{hash[c]-last_delete+1}, #{s[last_delete, hash[c]-last_delete+1]}"
2929
last_duplicate = hash[c] + 1
3030
s[last_delete, hash[c]+1].each_char do |c|
3131

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Use the sliding window method. i is the open, j is the close. shift i to j'+ 1 while s[j'] is the duplicate index of s[i,j]
2+
3+
str = 'adedefrsfrsfrgtgsfcdcscgtgsfdsccdcrgxgsz'
4+
str1= 'dvdf'
5+
str2= 'aaaaaaaaaaaaaa'
6+
str3= 'c'
7+
str4= 'axr'
8+
str5= 'aab'
9+
str6= 'cdabtred'
10+
str7= 'abba'
11+
str8= 'ohomm'
12+
13+
# @param {String} s
14+
# @return {Integer}
15+
def length_of_longest_substring(s)
16+
hash = Hash.new
17+
last_idx= s.length
18+
i=0
19+
j=0
20+
max=0
21+
while j < last_idx
22+
c=s[j]
23+
24+
if hash.has_key?(c)
25+
i = [hash[c],i].max
26+
end
27+
28+
len=j-i+1
29+
max = [max,len].max
30+
31+
hash[c]=j+1
32+
j=j+1
33+
34+
end
35+
max
36+
end
37+
38+
puts "Max length of #{str} = #{length_of_longest_substring(str)}"
39+
puts "Max length of #{str1} = #{length_of_longest_substring(str1)}"
40+
puts "Max length of #{str2} = #{length_of_longest_substring(str2)}"
41+
puts "Max length of #{str3} = #{length_of_longest_substring(str3)}"
42+
puts "Max length of #{str4} = #{length_of_longest_substring(str4)}"
43+
puts "Max length of #{str5} = #{length_of_longest_substring(str5)}"
44+
puts "Max length of #{str6} = #{length_of_longest_substring(str6)}"
45+
puts "Max length of #{str7} = #{length_of_longest_substring(str7)}"
46+
puts "Max length of #{str8} = #{length_of_longest_substring(str8)}"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ Median of Two Sorted Arrays
3030
[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)
3131

3232

33-
- Until 12/5
33+
- Until 12/5/2016
34+
35+
Methods involved
36+
37+
String/Characters
38+
- Sliding window
39+
- Hash

0 commit comments

Comments
 (0)