/* * @lc app=leetcode id=1374 lang=rust * * [1374] Generate a String With Characters That Have Odd Counts * * https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/description/ * * algorithms * Easy (77.71%) * Total Accepted: 10.1K * Total Submissions: 13K * Testcase Example: '4' * * Given an integer n, return a string with n characters such that each * character in such string occurs an odd number of times. * * The returned string must contain only lowercase English letters. If there * are multiples valid strings, return any of them.   * * * Example 1: * * * Input: n = 4 * Output: "pppz" * Explanation: "pppz" is a valid string since the character 'p' occurs three * times and the character 'z' occurs once. Note that there are many other * valid strings such as "ohhh" and "love". * * * Example 2: * * * Input: n = 2 * Output: "xy" * Explanation: "xy" is a valid string since the characters 'x' and 'y' occur * once. Note that there are many other valid strings such as "ag" and "ur". * * * Example 3: * * * Input: n = 7 * Output: "holasss" * * * * Constraints: * * * 1 <= n <= 500 * */ impl Solution { pub fn generate_the_string(n: i32) -> String { if n % 2 == 1 { std::iter::repeat("a").take(n as usize).collect::() } else {std::iter::repeat("a").take(n as usize - 1).collect::() + "b"} } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; use std::iter::FromIterator; use std::collections::VecDeque; use std::collections::BTreeMap; #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res } #[allow(dead_code)] pub fn vec_counter(arr: Vec) -> HashMap { let mut c = HashMap::new(); for n in arr { *c.entry(n).or_insert(0) += 1; } c } #[allow(dead_code)] pub fn vec_to_hashset(arr: Vec) -> HashSet { HashSet::from_iter(arr.iter().cloned()) }