From fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 Mon Sep 17 00:00:00 2001 From: HJ Date: Tue, 17 Jan 2023 12:46:04 -0500 Subject: adding old leetcode solutions --- leetcode/lc-1221-split-a-string-balanced.rkt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 leetcode/lc-1221-split-a-string-balanced.rkt (limited to 'leetcode/lc-1221-split-a-string-balanced.rkt') diff --git a/leetcode/lc-1221-split-a-string-balanced.rkt b/leetcode/lc-1221-split-a-string-balanced.rkt new file mode 100644 index 0000000..4c75770 --- /dev/null +++ b/leetcode/lc-1221-split-a-string-balanced.rkt @@ -0,0 +1,19 @@ +#lang racket +(require rackunit) + +(define/contract (balanced-string-split s) + (-> string? exact-integer?) + (for/fold ([acc 0] + [count 0] + #:result count) + ([c (string->list s)]) + (let* ([increment (case c + [(#\R) 1] + [(#\L) -1])] + [new-acc (+ increment acc)] + [new-count (case new-acc + [(0) (add1 count)] + [else count])]) + (values new-acc new-count)))) + +(check-eq? (balanced-string-split "RLRRLLRLRL") 4) \ No newline at end of file -- cgit v1.2.3