aboutsummaryrefslogtreecommitdiff
path: root/racket/leetcode/lc-745-prefix-suffix.rkt
diff options
context:
space:
mode:
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