Commit 31a2d66

mo khan <mo.khan@gmail.com>
2020-08-11 21:34:29
Detect off by one and end of string
1 parent 1b999e4
Changed files (1)
2020
08
2020/08/11/main.rb
@@ -6,22 +6,22 @@ end
 
 class Solution
   def self.run(string)
-    max = 0
-    current = 0
+    max, current = 0, 0
 
-    0.upto(string.chars.size) do |i|
+    0.upto(string.size - 1) do |i|
       if string[i] == string[i-1]
         max = max > current ? max : current
         current = 0
       end
-      current = current + 1
+      current += 1
     end
 
-    max
+    max > current ? max : current
   end
 end
 
 assert_equal Solution.run('abrkaabcdefghijjxxx'), 10
+assert_equal Solution.run('aaabcd'), 4
 
 # step 1. understand the problem
 # step 2. pick a solution