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

Source code in the rust programming language

fn unique(s: &str) -> Option<(usize, usize, char)> {
    s.chars().enumerate().find_map(|(i, c)| {
        s.chars()
            .enumerate()
            .skip(i + 1)
            .find(|(_, other)| c == *other)
            .map(|(j, _)| (i, j, c))
    })
}

fn main() {
    let strings = [
        "",
        ".",
        "abcABC",
        "XYZ ZYX",
        "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ",
        "01234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ0X",
        "hétérogénéité",
        "🎆🎃🎇🎈",
        "😍😀🙌💃😍🙌",
        "🐠🐟🐡🦈🐬🐳🐋🐡",
    ];

    for string in &strings {
        print!("\"{}\" (length {})", string, string.chars().count());
        match unique(string) {
            None => println!(" is unique"),
            Some((i, j, c)) => println!(
                " is not unique\n\tfirst duplicate: \"{}\" (U+{:0>4X}) at indices {} and {}",
                c, c as usize, i, j
            ),
        }
    }
}


  

You may also check:How to resolve the algorithm Metaprogramming step by step in the C programming language
You may also check:How to resolve the algorithm Minkowski question-mark function step by step in the Phix programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the VBA programming language
You may also check:How to resolve the algorithm Special characters step by step in the jq programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the Scala programming language