How to resolve the algorithm Determine if a string has all unique characters step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string has all unique characters step by step in the Racket programming language

Table of Contents

Problem Statement

Given a character string   (which may be empty, or have a length of zero characters):

Use (at least) these five test values   (strings):

Show all output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string has all unique characters step by step in the Racket programming language

Source code in the racket programming language

#lang racket

(define (first-non-unique-element.index seq)
  (let/ec ret
    (for/fold ((es (hash))) ((e seq) (i (in-naturals)))
      (if (hash-has-key? es e) (ret (list e (hash-ref es e) i)) (hash-set es e i)))
    #f))

(define (report-if-a-string-has-all-unique-characters str)
  (printf "~s (length ~a): ~a~%" str (string-length str)
          (match (first-non-unique-element.index str)
            [#f "contains all unique characters"]
            [(list e i i′) (format "has character '~a' (0x~a) at index ~a (first seen at ~a)"
                                   e (number->string (char->integer e) 16) i′ i)])))

(module+ main
  (for-each report-if-a-string-has-all-unique-characters
            (list "" "." "abcABC" "XYZ ZYX"
                  "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")))


  

You may also check:How to resolve the algorithm Balanced brackets step by step in the BQN programming language
You may also check:How to resolve the algorithm Read entire file step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm A+B step by step in the Euler programming language
You may also check:How to resolve the algorithm Copy a string step by step in the ARM Assembly programming language