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 --- .../lc-1844-replace-all-digits-with-characters.rkt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 leetcode/lc-1844-replace-all-digits-with-characters.rkt (limited to 'leetcode/lc-1844-replace-all-digits-with-characters.rkt') diff --git a/leetcode/lc-1844-replace-all-digits-with-characters.rkt b/leetcode/lc-1844-replace-all-digits-with-characters.rkt new file mode 100644 index 0000000..96aba6e --- /dev/null +++ b/leetcode/lc-1844-replace-all-digits-with-characters.rkt @@ -0,0 +1,18 @@ +#lang racket +(define/contract (replace-digits s) + (-> string? string?) + (define/contract (shift-letter c x) + (-> char? char? char?) + (integer->char (+ (string->number (string x)) (char->integer c)))) + (define letters (string->list (string-replace s #rx"[0-9]" ""))) + (define digits (string->list (string-replace s #rx"[a-z]" ""))) + (foldl (λ (c x acc) + (if (equal? x #\X) + (string-append acc (string c)) + (string-append acc (string c) (string (shift-letter c x))))) + "" + letters + (if (= (length digits) (length letters)) + digits + (append digits '(#\X))) + )) \ No newline at end of file -- cgit v1.2.3