Commit 5ad66fb
Changed files (1)
2020
08
12
2020/08/12/main.rb
@@ -0,0 +1,73 @@
+=begin
+two pointer:
+* left
+* right
+
+left moves ->
+right moves <-
+
+* they move towards each other until they meet.
+* if all characters are the same while they move towards each
+other, then we have a palindrome.
+
+ l r
+|b|a|n|a|n|a|
+
+
+|b|a|
+|b|a|n|
+|b|a|n|a|
+|b|a|n|a|n|
+|b|a|n|a|n|a|
+
+|a|n|
+|a|n|a|
+|a|n|a|n|
+|a|n|a|n|a|
+
+|n|a|
+|n|a|n|
+|n|a|n|a|
+
+|a|n|a|
+
+recursive solution
+base case:
+
+* s is empty
+
+if s[0] == s[-1]
+ run(s[1..-2])
+end
+=end
+
+def assert_equal(x, y)
+ return puts "PASS" if x == y
+ puts "FAIL: expected #{x}, got #{y}"
+end
+
+class Solution
+ def self.palindrome?(string)
+ return "" if string.nil? || string.empty?
+
+ string == string.reverse ? string : ""
+ end
+
+ def self.run(string)
+ max = ""
+
+ for l in (0..string.size)
+ for r in (l+1..string.size)
+ result = palindrome?(string[l..r])
+ max = result if result.size > max.size
+ end
+ end
+
+ max
+ end
+end
+
+assert_equal 'anana', Solution.run('banana')
+assert_equal 'illi', Solution.run('million')
+assert_equal 'racecar', Solution.run('tracecars')
+assert_equal 'tacocat', Solution.run('loltacocatmom')