From 3f326e31859a217a3bc3b988e0bb72a32cfe8061 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 24 Aug 2024 11:52:03 +0800 Subject: [PATCH] 20240824 finished. --- commit.sh | 11 ----- justfile | 20 ++++++++++ src/problem/mod.rs | 3 +- ...mutation_difference_between_two_strings.rs | 40 +++++++++++++++++++ 4 files changed, 62 insertions(+), 12 deletions(-) delete mode 100755 commit.sh create mode 100755 justfile create mode 100644 src/problem/p3146_permutation_difference_between_two_strings.rs diff --git a/commit.sh b/commit.sh deleted file mode 100755 index 506d674..0000000 --- a/commit.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -set -e - -time=$(date "+%Y%m%d") -message="$time Finished" - -git add -A -git commit -m "$message" - -git push diff --git a/justfile b/justfile new file mode 100755 index 0000000..e837ea8 --- /dev/null +++ b/justfile @@ -0,0 +1,20 @@ +#!/usr/bin/env just --justfile + +build: + cargo build --release + +test: + cargo test + +commit: + #!/usr/bin/env bash + set -euxo pipefail + time=$(date "+%Y%m%d") + message="$time finished." + + git add -A + git commit -m "$message" + git push + +pull id: build + ./target/release/leetcode-rust {{id}} \ No newline at end of file diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 8d01248..cebddd2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -214,4 +214,5 @@ mod p552_student_attendance_record_ii; mod p3154_find_number_of_ways_to_reach_the_k_th_stair; mod p3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k; mod p3133_minimum_array_end; -mod p3145_find_products_of_elements_of_big_array; \ No newline at end of file +mod p3145_find_products_of_elements_of_big_array; +mod p3146_permutation_difference_between_two_strings; \ No newline at end of file diff --git a/src/problem/p3146_permutation_difference_between_two_strings.rs b/src/problem/p3146_permutation_difference_between_two_strings.rs new file mode 100644 index 0000000..93f5d2c --- /dev/null +++ b/src/problem/p3146_permutation_difference_between_two_strings.rs @@ -0,0 +1,40 @@ +/** + * [3146] Permutation Difference between Two Strings + */ +pub struct Solution {} + + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn find_permutation_difference(s: String, t: String) -> i32 { + let mut map = HashMap::new(); + + for (i, c) in s.chars().enumerate() { + map.insert(c, i as i32); + } + + let mut result = 0; + + for (i, c) in t.chars().enumerate() { + let target = map.get(&c).unwrap(); + + result += (i as i32 - *target).abs() + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3146() { + assert_eq!(2, Solution::find_permutation_difference("abc".to_owned(), "bac".to_owned())); + } +}