Skip to content

Instantly share code, notes, and snippets.

@ninjapanzer
Created April 22, 2015 02:13
Show Gist options
  • Select an option

  • Save ninjapanzer/a1016648431903ef7c11 to your computer and use it in GitHub Desktop.

Select an option

Save ninjapanzer/a1016648431903ef7c11 to your computer and use it in GitHub Desktop.
Hacker Rank Finding matching character sequences
% Enter your code here. Read input from STDIN. Print output to STDOUT
% Your class should be named solution
-module(count_repeate_characters).
-export([main/0]).
main() ->
{inputs, STRINGS} = inputs(),
DELETES = find_deletes(STRINGS),
outputs(DELETES).
find_deletes(STRINGS) ->
deletes(STRINGS, []).
deletes([], DELETES) -> DELETES;
deletes([H|T], DELETES) ->
DELETES1 = [count_deletes(H, 0)] ++ DELETES,
deletes(T, DELETES1).
count_deletes([], PAIRS) -> PAIRS;
count_deletes([_], PAIRS) -> PAIRS;
count_deletes([H|T], PAIRS) ->
[H1|_] = T,
if H == H1 ->
PAIRS1 = PAIRS + 1;
true ->
PAIRS1 = PAIRS
end,
count_deletes(T, PAIRS1).
inputs() ->
{ok, [COUNT|_]} = io:fread("", "~d"),
read_strings(COUNT, []).
outputs([]) -> [];
outputs([H|T]) ->
log(H),
outputs(T).
read_strings(0, STRINGS) -> {inputs, STRINGS};
read_strings(TIMES, STRINGS) ->
STRINGS1 = [io:get_line("")] ++ STRINGS,
read_strings(TIMES - 1, STRINGS1).
log(MSG) -> io:format("~w~n",[MSG]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment