aboutsummaryrefslogtreecommitdiff
path: root/racket/leetcode/lc-745-prefix-suffix.rkt
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/leetcode/lc-745-prefix-suffix.rkt
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'racket/leetcode/lc-745-prefix-suffix.rkt')
-rw-r--r--racket/leetcode/lc-745-prefix-suffix.rkt27
1 files changed, 27 insertions, 0 deletions
diff --git a/racket/leetcode/lc-745-prefix-suffix.rkt b/racket/leetcode/lc-745-prefix-suffix.rkt
new file mode 100644
index 0000000..b01e3bb
--- /dev/null
+++ b/racket/leetcode/lc-745-prefix-suffix.rkt
@@ -0,0 +1,27 @@
+#lang racket
+(define word-filter%
+ (class object%
+ (super-new)
+
+ ; words : (listof string?)
+ (init-field
+ words) ; Take in the provided dictionary.
+ (define word-ends-hash (make-hash)) ; Make an empty hash table.
+
+ (for/list ([w (in-list words)] ; For each word in the dictionary,
+ [index (in-naturals)]) ; and its corresponding index,
+ (define len (string-length w)) ; calculate its length,
+ (for*/list ([head (in-range 1 (min 11 (add1 len)))] ; and for every combination of head length
+ [tail (in-range 1 (min 11 (add1 len)))]) ; and tail length
+ (hash-set! word-ends-hash ; from 1 to the max. affix length,
+ (list (substring w 0 head) ; set the key to the list containing
+ (substring w (- len tail) len)) ; the prefix and suffix
+ index))) ; and map it to the word's index.
+
+ ; f : string? string? -> exact-integer?
+ (define/public (f prefix suffix) ; Return the mapped value for the affixes
+ (hash-ref word-ends-hash (list prefix suffix) -1)))) ; or -1 if it doesn't exist.
+
+;; Your word-filter% object will be instantiated and called as such:
+;; (define obj (new word-filter% [words words]))
+;; (define param_1 (send obj f prefix suffix)) \ No newline at end of file