How to resolve the algorithm Determine if a string has all unique characters step by step in the OCaml 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 OCaml 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 OCaml programming language

Source code in the ocaml programming language

module CMap = Map.Make(struct
  type t = char
  let compare = compare
end)

(** Add index as argument to string.fold_left *)
let string_fold_left_i f acc str =
  snd (String.fold_left
    (fun (index, acc) char -> (index+1, f acc index char))
    (0, acc) str)

exception Found of int * int * char

let has_duplicates str =
  try let _ = string_fold_left_i
    (fun map index char ->
      match CMap.find_opt char map with
        | None -> CMap.add char index map
        | Some i -> raise (Found (i,index,char)))
    CMap.empty str
    in Ok ()
  with Found (i,j,c) -> Error (i,j,c)

let printer str =
  Format.printf "%S (len %d) : " str (String.length str);
  match has_duplicates str with
  | Ok () -> Format.printf "No duplicates.\n"
  | Error (i,j,c) -> Format.printf "Duplicate '%c' (%#x) at %d and %d\n" c (int_of_char c) i j

let () =
  printer "";
  printer ".";
  printer "abcABC";
  printer "XYZ ZYX";
  printer "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"


  

You may also check:How to resolve the algorithm Execute Brain step by step in the Ada programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the SparForte programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Terminal control/Preserve screen step by step in the Emacs Lisp programming language