Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to print the characters need to be removed so that two strings become anagram in Rust

1 Answer

0 votes
//use std::collections::HashMap;

const TOTAL_ABC_LETTERS: usize = 26;

fn print_characters_need_to_be_removed_for_anagram(str1: &str, str2: &str) {
    let mut count1 = vec![0; TOTAL_ABC_LETTERS];
    let mut count2 = vec![0; TOTAL_ABC_LETTERS];

    // count char frequency str1
    for ch in str1.chars() {
        count1[ch as usize - 'a' as usize] += 1;
    }

    // count char frequency str2
    for ch in str2.chars() {
        count2[ch as usize - 'a' as usize] += 1;
    }

    for i in 0..TOTAL_ABC_LETTERS {
        if (count1[i] as isize - count2[i] as isize).abs() > 0 {
            print!("{} ", (i as u8 + 'a' as u8) as char);
        }
    }
}

fn main() {
    let str1 = "masterfx";
    let str2 = "ksampret";

    print_characters_need_to_be_removed_for_anagram(str1, str2);
}


   
/*
run:
  
f k p x 
  
*/

 



answered Nov 30, 2024 by avibootz
...