% https://open.kattis.com/problems/peragrams % 1s % 1.9 Easy % Using Picat for some excursions on this. % For an anagram of a string of % * even length: there must be an even occurrences % of all chars % * odd length: there must be exactly one char % with an odd number of occurrences and % all the rest must have even occurrences main :- S = read_file_lines().first, println(S), Ss = S.sort, clumped(Ss,Cl), writeln(cl=Cl), if anagram(S) then println(ok) else println(not_ok) end, nl. % anagram(S) :- Len = S.len, println(len=Len), H = S.occurrences(), println(h=H), K = H.values.occurrences(), println(k=K), M = [Occ mod 2 : Occ in H.values.sort].occurrences, println(m=M), if Len mod 2 == 1 then % Odd length: % - Exactly one occurrenc with odd occurrence % - All the rest must be even occurrence M.get(1,0) == 1, else % Even length: % - add öemgths must be even (i.e. no is odd) M.get(1,0) == 0 end, nl. occurrences(H) = Occ => Occ = new_map(), foreach(E in H) Occ.put(E,Occ.get(E,0)+1) end. clumped(Items, Counts) :- clump(Items, Counts). clump([], []). clump([H|T0], [H-C|T]) :- ccount(T0, H, T1, 1, C), clump(T1, T). ccount([H|T0], E, T, C0, C) :- E==H, !, C1 is C0+1, ccount(T0, E, T, C1, C). ccount(List, _, List, C, C).