From 1a12e0db0c196a36e7d22b660fd475f54d7cc33a Mon Sep 17 00:00:00 2001 From: Divlo Date: Thu, 5 Jan 2023 19:01:25 +0100 Subject: [PATCH] feat(solutions): add `first-non-repeating-character/rust/function` --- .../solutions/rust/function/Cargo.lock | 7 +++ .../solutions/rust/function/Cargo.toml | 6 ++ .../solutions/rust/function/README.md | 3 + .../solutions/rust/function/src/main.rs | 63 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 challenges/first-non-repeating-character/solutions/rust/function/Cargo.lock create mode 100644 challenges/first-non-repeating-character/solutions/rust/function/Cargo.toml create mode 100644 challenges/first-non-repeating-character/solutions/rust/function/README.md create mode 100644 challenges/first-non-repeating-character/solutions/rust/function/src/main.rs diff --git a/challenges/first-non-repeating-character/solutions/rust/function/Cargo.lock b/challenges/first-non-repeating-character/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/first-non-repeating-character/solutions/rust/function/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_application" +version = "1.0.0" diff --git a/challenges/first-non-repeating-character/solutions/rust/function/Cargo.toml b/challenges/first-non-repeating-character/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/first-non-repeating-character/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/first-non-repeating-character/solutions/rust/function/README.md b/challenges/first-non-repeating-character/solutions/rust/function/README.md new file mode 100644 index 0000000..4a0cd77 --- /dev/null +++ b/challenges/first-non-repeating-character/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# first-non-repeating-character/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 5 January 2023. diff --git a/challenges/first-non-repeating-character/solutions/rust/function/src/main.rs b/challenges/first-non-repeating-character/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..352de4d --- /dev/null +++ b/challenges/first-non-repeating-character/solutions/rust/function/src/main.rs @@ -0,0 +1,63 @@ +use std::{collections::HashMap, io}; + +#[derive(Debug, Copy, Clone)] +pub struct CharacterOccurence { + pub total: usize, + pub first_index: usize, + pub value: char, +} + +pub fn first_non_repeating_character(string: &str) -> String { + let mut characters_occurences: HashMap = HashMap::new(); + for (index, character) in string.chars().enumerate() { + match characters_occurences.get(&character) { + Some(value) => { + characters_occurences.insert( + character, + CharacterOccurence { + total: value.total + 1, + ..value.clone() + }, + ); + } + None => { + characters_occurences.insert( + character, + CharacterOccurence { + total: 1, + first_index: index, + value: character, + }, + ); + } + } + } + let mut result: Option = Option::None; + for (_, character_occurence) in &characters_occurences { + if character_occurence.total == 1 { + match result { + Some(ref mut result) => { + if character_occurence.first_index < result.first_index { + *result = *character_occurence; + } + } + None => { + result = Some(*character_occurence); + } + } + } + } + match result { + Some(result) => result.value.to_string(), + None => "".to_string(), + } +} + +fn main() { + let mut string = String::new(); + io::stdin() + .read_line(&mut string) + .expect("Failed to read `stdin` line."); + let string = string.trim(); + println!("{}", first_non_repeating_character(&string)); +}