-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0809-expressive-words.rkt
49 lines (41 loc) · 1.6 KB
/
0809-expressive-words.rkt
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
37
38
39
40
41
42
43
44
45
46
47
48
49
#lang racket
; (define (string->pairs s)
; (define (iter i c sl res)
; (cond [(null? sl) (cons (cons c i) res)]
; [(or (false? c) (equal? c (car sl))) (iter (add1 i) (car sl) (cdr sl) res)]
; [else (iter 0 #f sl (cons (cons c i) res))]))
; (let ([slist (string->list s)])
; (iter 0 #f slist empty)))
(define (extract-pair i c s)
(if (or (null? s) (not (equal? c (car s))))
(values i s)
(extract-pair (add1 i) c (cdr s))))
(define (string->pairs s)
(define (iter s res)
(if (null? s)
res
(let ([c (car s)])
(let-values ([(i ss) (extract-pair 1 c (cdr s))])
(iter ss (cons (cons c i) res))))))
(iter (string->list s) empty))
(define (expressive? spairs wpairs)
(let ([sn (null? spairs)]
[wn (null? wpairs)])
(cond [(and sn wn) true]
[(not (or sn wn))
(and (equal? (caar spairs) (caar wpairs))
(or (= (cdar spairs) (cdar wpairs))
(>= (cdar spairs) (max (cdar wpairs) 3)))
(expressive? (cdr spairs) (cdr wpairs)))]
[else false])))
(define/contract (expressive-words s words)
(-> string? (listof string?) exact-integer?)
(let* ([spairs (string->pairs s)]
[sexpressive? (lambda (wpairs) (expressive? spairs wpairs))])
(count sexpressive? (map string->pairs words))))
(string->pairs "dddiiiinnssssssoooo")
(expressive? (string->pairs "dddiiiinnssssssoooo")
(string->pairs "ddiinnso"))
(expressive-words
"dddiiiinnssssssoooo"
'("dinnssoo" "ddinso" "ddiinnso" "ddiinnssoo" "ddiinso" "dinsoo" "ddiinsso" "dinssoo" "dinso"))