3146. Permutation Difference between Two Strings Easy

@problem@discussion
#Hash Table#String



1/**
2 * [3146] Permutation Difference between Two Strings
3 *
4 * You are given two strings s and t such that every character occurs at most once in s and t is a permutation of s.
5 * The permutation difference between s and t is defined as the sum of the absolute difference between the index of the occurrence of each character in s and the index of the occurrence of the same character in t.
6 * Return the permutation difference between s and t.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "abc", t = "bac"</span>
11 * Output: <span class="example-io">2</span>
12 * Explanation:
13 * For s = "abc" and t = "bac", the permutation difference of s and t is equal to the sum of:
14 * 
15 * 	The absolute difference between the index of the occurrence of "a" in s and the index of the occurrence of "a" in t.
16 * 	The absolute difference between the index of the occurrence of "b" in s and the index of the occurrence of "b" in t.
17 * 	The absolute difference between the index of the occurrence of "c" in s and the index of the occurrence of "c" in t.
18 * 
19 * That is, the permutation difference between s and t is equal to |0 - 1| + |1 - 0| + |2 - 2| = 2.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">s = "abcde", t = "edbac"</span>
24 * Output: <span class="example-io">12</span>
25 * Explanation: The permutation difference between s and t is equal to |0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12.
26 * </div>
27 *  
28 * Constraints:
29 * 
30 * 	1 <= s.length <= 26
31 * 	Each character occurs at most once in s.
32 * 	t is a permutation of s.
33 * 	s consists only of lowercase English letters.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/permutation-difference-between-two-strings/
39// discuss: https://leetcode.com/problems/permutation-difference-between-two-strings/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn find_permutation_difference(s: String, t: String) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3146() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.