File tree 3 files changed +56
-4
lines changed
Longest Substring Without Repeating Characters
3 files changed +56
-4
lines changed Original file line number Diff line number Diff line change @@ -18,14 +18,14 @@ def length_of_longest_substring(s)
18
18
max = 0
19
19
s . each_char . with_index do |c , idx |
20
20
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}"
22
22
hash [ c ] = idx
23
23
else
24
24
if idx -hash [ c ] > max
25
25
max = idx -hash [ c ]
26
26
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]}"
29
29
last_duplicate = hash [ c ] + 1
30
30
s [ last_delete , hash [ c ] +1 ] . each_char do |c |
31
31
Original file line number Diff line number Diff line change
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 ) } "
Original file line number Diff line number Diff line change @@ -30,4 +30,10 @@ Median of Two Sorted Arrays
30
30
[ Construct Binary Tree from Inorder and Postorder Traversal] ( https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ )
31
31
32
32
33
- - Until 12/5
33
+ - Until 12/5/2016
34
+
35
+ Methods involved
36
+
37
+ String/Characters
38
+ - Sliding window
39
+ - Hash
You can’t perform that action at this time.
0 commit comments