LeetCode 1153. String Transforms Into Another String

Problem Statement


use std::collections::{HashMap, HashSet};

impl Solution {
pub fn can_convert(str1: String, str2: String) -> bool {
if str1.len() != str2.len() {
return false;
}
if str1 == str2 {
return true;
}

let mut map = HashMap::new();
let mut have = HashSet::new();

for i in 0..str1.len() {
let (key, val) = (str1.chars().nth(i).unwrap(), str2.chars().nth(i).unwrap());
have.insert(val);
if !map.contains_key(&key) {
map.insert(key, val);
} else if *map.get(&key).unwrap() != val {
return false;
}
}

return map.len() >= have.len() && (have.len() < 26);
}
}

LeetCode 1155. Number of Dice Rolls With Target Sum

Problem Statement


impl Solution {
pub fn num_rolls_to_target(d: i32, f: i32, target: i32) -> i32 {
const MOD : u64 = 1e9 as u64 + 7;
let mut dp = vec![vec![0; target as usize + 1]; d as usize + 1];
dp[0][0] = 1;
for i in 1..d as usize + 1 {
for j in 1..f as usize + 1 {
for k in j..target as usize + 1 {
dp[i][k] = (dp[i][k] + dp[i - 1][k - j]) % MOD;
}
}
}
return dp[d as usize][target as usize] as i32;
}
}