blob: 76449b86b3ebf42a7e7168999079f3fcc9684364 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/usr/bin/env elixir
defmodule PWC do
def twiceAppearance([], _), do: "␀" # fallback
def twiceAppearance([next | rest], count) do
if Map.has_key?(count, next) do
next
else
twiceAppearance(rest, Map.put(count, next, 1))
end
end
def twiceAppearance(str) do
# split the string into characters and
# reprocess with an empty Map
twiceAppearance(String.graphemes(str), %{})
end
def solution(str) do
IO.puts("Input: $str = \"#{str}\"")
IO.puts("Output: \"#{twiceAppearance(str)}\"")
end
end
IO.puts("Example 1:")
PWC.solution("acbddbca")
IO.puts("\nExample 2:")
PWC.solution("abccd")
IO.puts("\nExample 3:")
PWC.solution("abcdabbb")
IO.puts("\nExample 4:")
PWC.solution("abcdefg")
|