Skip to content

Commit

Permalink
longer contigous substed of ones and zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
xsami committed Jan 6, 2024
1 parent 1b5daea commit 0eecb8d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LongestSegments/longestSegment.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Solution for: https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/description/
defmodule Solution do

defp get_longest_count([], _, _, max_sum), do: max_sum

defp get_longest_count([head | tail], char, acc, max_sum) do
if head != char do
get_longest_count(tail, char, 0, max(max_sum, acc))
else
get_longest_count(tail, char, acc + 1, max(max_sum, acc + 1))
end
end

@spec check_zero_ones(s :: String.t) :: boolean
def check_zero_ones(s) do
sList = String.split(s, "")
get_longest_count(sList, "1", 0, 0) > get_longest_count(sList, "0", 0, 0)
end
end

0 comments on commit 0eecb8d

Please sign in to comment.