11module
2+ import Std.Data.Iterators.Consumers.Set
3+ import Std.Data.Iterators.Lemmas.Consumers.Set
4+ import Std.Data.HashSet.Lemmas
25
3- def count_distinct_characters : Unit :=
4- ()
6+ open Std
7+
8+ def countDistinctCharacters (s : String) : Nat :=
9+ (s.chars.map Char.toLower).toHashSet.size
10+
11+ example : countDistinctCharacters "" = 0 := by native_decide
12+ example : countDistinctCharacters "abcde" = 5 := by native_decide
13+ example : countDistinctCharacters ("abcde" ++ "cade" ++ "CADE" ) = 5 := by native_decide
14+ example : countDistinctCharacters "aaaaAAAAaaaa" = 1 := by native_decide
15+ example : countDistinctCharacters "Jerry jERRY JeRRRY" = 5 := by native_decide
16+
17+ theorem countDistinctCharacters_eq {s : String} :
18+ countDistinctCharacters s = (HashSet.ofList (s.toList.map Char.toLower)).size := by
19+ simp [countDistinctCharacters, Iter.toHashSet_equiv_ofList.size_eq]
20+
21+ theorem countDistinctCharacters_empty : countDistinctCharacters "" = 0 := by
22+ simp [countDistinctCharacters_eq]
23+
24+ theorem countDistinctCharacters_push {s : String} {c : Char} :
25+ countDistinctCharacters (s.push c) =
26+ if c.toLower ∈ s.toList.map Char.toLower then
27+ countDistinctCharacters s
28+ else
29+ countDistinctCharacters s + 1 := by
30+ simp only [countDistinctCharacters_eq, String.toList_push, List.map_append, List.map_cons,
31+ List.map_nil, List.mem_map]
32+ rw [HashSet.ofList_equiv_foldl.size_eq, List.foldl_append, List.foldl_cons, List.foldl_nil,
33+ HashSet.size_insert]
34+ simp [← HashSet.ofList_equiv_foldl.mem_iff, ← HashSet.ofList_equiv_foldl.size_eq]
535
636/-!
737## Prompt
@@ -42,4 +72,4 @@ def check(candidate):
4272 assert candidate('aaaaAAAAaaaa') == 1
4373 assert candidate('Jerry jERRY JeRRRY') == 5
4474```
45- -/
75+ -/
0 commit comments