diff --git a/ladderz/all.html b/ladderz/all.html index 225c4d9..91c5ed6 100644 --- a/ladderz/all.html +++ b/ladderz/all.html @@ -1,2 +1,2 @@ List of all items in this crate -

List of all items

Functions

\ No newline at end of file +

List of all items

Functions

\ No newline at end of file diff --git a/ladderz/dsa/fn.is_anagram.html b/ladderz/dsa/fn.is_anagram.html new file mode 100644 index 0000000..012d26a --- /dev/null +++ b/ladderz/dsa/fn.is_anagram.html @@ -0,0 +1,2 @@ +is_anagram in ladderz::dsa - Rust +

Function ladderz::dsa::is_anagram

source ·
pub fn is_anagram(a: String, b: String) -> bool
\ No newline at end of file diff --git a/ladderz/dsa/fn.is_anagram2.html b/ladderz/dsa/fn.is_anagram2.html new file mode 100644 index 0000000..577aeae --- /dev/null +++ b/ladderz/dsa/fn.is_anagram2.html @@ -0,0 +1,2 @@ +is_anagram2 in ladderz::dsa - Rust +

Function ladderz::dsa::is_anagram2

source ·
pub fn is_anagram2(a: String, b: String) -> bool
\ No newline at end of file diff --git a/ladderz/dsa/index.html b/ladderz/dsa/index.html index a2a0508..65ffd05 100644 --- a/ladderz/dsa/index.html +++ b/ladderz/dsa/index.html @@ -1,5 +1,5 @@ ladderz::dsa - Rust -

Module ladderz::dsa

source ·
Expand description

Various data structures and algorithms implementations.

+

Module ladderz::dsa

source ·
Expand description

Various data structures and algorithms implementations.

Example

use ladderz::dsa::contains_duplicate;
 
@@ -7,4 +7,4 @@
 let result = contains_duplicate(nums.clone());
 println!("The vector {:?} {} contain a duplicate.", &nums, if result { "does" } else { "does not" });
The vector [2, 3, 4, 2] does contain a duplicate.
-

Functions

\ No newline at end of file +

Functions

\ No newline at end of file diff --git a/ladderz/dsa/sidebar-items.js b/ladderz/dsa/sidebar-items.js index 2faebcc..9a2f7e6 100644 --- a/ladderz/dsa/sidebar-items.js +++ b/ladderz/dsa/sidebar-items.js @@ -1 +1 @@ -window.SIDEBAR_ITEMS = {"fn":["contains_duplicate"]}; \ No newline at end of file +window.SIDEBAR_ITEMS = {"fn":["contains_duplicate","is_anagram","is_anagram2"]}; \ No newline at end of file diff --git a/lz/dsa/enum.Dsa.html b/lz/dsa/enum.Dsa.html index f7ab5f5..e8ab4a3 100644 --- a/lz/dsa/enum.Dsa.html +++ b/lz/dsa/enum.Dsa.html @@ -1,9 +1,14 @@ -Dsa in lz::dsa - Rust -

Enum lz::dsa::Dsa

source ·
pub enum Dsa {
+Dsa in lz::dsa - Rust
+    

Enum lz::dsa::Dsa

source ·
pub enum Dsa {
     ContainsDuplicate {
         n: Vec<i32>,
         raw: bool,
     },
+    IsAnagram {
+        a: String,
+        b: String,
+        raw: bool,
+    },
 }

Variants§

§

ContainsDuplicate

Fields

§n: Vec<i32>

The vector of numbers to detect whether it has a duplicate.

§raw: bool

Whether or not to return the raw output.

Returns true or false based on whether the vector has a duplicate.

@@ -12,6 +17,13 @@ lz dsa contains-duplicate 1 2 3 2
Output
The vector [1, 2, 3, 2] does contain a duplicate.
 
Raw Output (use -r or --raw)
true
+
§

IsAnagram

Fields

§a: String

The first string to compare against.

+
§b: String

The second string to compare against.

+
§raw: bool

Whether or not to return the raw output.

+

Returns true or false based on whether string a is an anagram of string b.

+
Example
Input
lz dsa is-anagram marc cram
+
Output
"marc" is an anagram of "cram".
+
Raw Output (use -r or --raw)
true
 

Trait Implementations§

source§

impl FromArgMatches for Dsa

source§

fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from [ArgMatches], parsing the arguments as needed. Read more
source§

fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches ) -> Result<Self, Error>

Instantiate Self from [ArgMatches], parsing the arguments as needed. Read more
source§

fn update_from_arg_matches( diff --git a/lz/dsa/fn.match_dsa.html b/lz/dsa/fn.match_dsa.html index 09a5cbe..dad61ae 100644 --- a/lz/dsa/fn.match_dsa.html +++ b/lz/dsa/fn.match_dsa.html @@ -1,2 +1,2 @@ match_dsa in lz::dsa - Rust -

Function lz::dsa::match_dsa

source ·
pub fn match_dsa(function: Option<Dsa>)
\ No newline at end of file +

Function lz::dsa::match_dsa

source ·
pub fn match_dsa(function: Option<Dsa>)
\ No newline at end of file diff --git a/lz/dsa/index.html b/lz/dsa/index.html index f95cfdb..33e1337 100644 --- a/lz/dsa/index.html +++ b/lz/dsa/index.html @@ -1,2 +1,2 @@ lz::dsa - Rust -
\ No newline at end of file +
\ No newline at end of file diff --git a/search-index.js b/search-index.js index 59dac3b..f9ca1c5 100644 --- a/search-index.js +++ b/search-index.js @@ -1,6 +1,6 @@ var searchIndex = new Map(JSON.parse('[\ -["ladderz",{"doc":"ladderz","t":"CCHHHHHHHHHH","n":["dsa","prealgebra","contains_duplicate","get_factor_pairs","get_factors","get_multiples_in_range","get_prime_factorization","get_primes_in_range","is_composite","is_factor","is_multiple","is_prime"],"q":[[0,"ladderz"],[2,"ladderz::dsa"],[3,"ladderz::prealgebra"],[12,"alloc::vec"],[13,"std::collections::hash::set"],[14,"std::collections::hash::map"]],"d":["Various data structures and algorithms implementations.","Various pre-algebra implementations including factor …","","Finds all factor pairs for a positive integer n.","Finds all factors of a positive integer n.","Finds all the multiples of a positive integer n starting …","Returns the prime factorization of a positive integer n.","Returns all prime numbers in the range [start, end].","Checks if a positive integer n is a composite number.","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …","Checks if a positive integer n is a prime number."],"i":[0,0,0,0,0,0,0,0,0,0,0,0],"f":[0,0,[[[2,[1]]],3],[4,[[6,[[5,[4,4]]]]]],[4,[[6,[4]]]],[[4,4,4],[[6,[4]]]],[4,[[7,[4,4]]]],[[4,4],[[6,[4]]]],[4,3],[[4,4],3],[[4,4],3],[4,3]],"c":[],"p":[[1,"i32"],[5,"Vec",12],[1,"bool"],[1,"u32"],[1,"tuple"],[5,"HashSet",13],[5,"HashMap",14]],"b":[]}],\ -["lz",{"doc":"lz","t":"FPPGNNNNNNNNNNCNNNNNNNNNNHCONNNNNNNNNNOOPGNNNNNNNNNHNNNNNOOPPPPPPPGPPNNNNNNNNNHNNNNNOOOOOOOOOOOOOOOOOOOOOOO","n":["Cli","Dsa","Prealgebra","Subjects","augment_args","augment_args_for_update","augment_subcommands","augment_subcommands_for_update","borrow","borrow","borrow_mut","borrow_mut","command","command_for_update","dsa","from","from","from_arg_matches","from_arg_matches","from_arg_matches_mut","from_arg_matches_mut","group_id","has_subcommand","into","into","main","prealgebra","subject","try_from","try_from","try_into","try_into","type_id","type_id","update_from_arg_matches","update_from_arg_matches","update_from_arg_matches_mut","update_from_arg_matches_mut","function","function","ContainsDuplicate","Dsa","augment_subcommands","augment_subcommands_for_update","borrow","borrow_mut","from","from_arg_matches","from_arg_matches_mut","has_subcommand","into","match_dsa","try_from","try_into","type_id","update_from_arg_matches","update_from_arg_matches_mut","n","raw","FactorPairs","Factors","IsComposite","IsFactor","IsMultiple","IsPrime","MultiplesInRange","Prealgebra","PrimeFactorization","PrimesInRange","augment_subcommands","augment_subcommands_for_update","borrow","borrow_mut","from","from_arg_matches","from_arg_matches_mut","has_subcommand","into","match_prealgebra","try_from","try_into","type_id","update_from_arg_matches","update_from_arg_matches_mut","lower_bound","lower_bound","m","m","n","n","n","n","n","n","n","n","raw","raw","raw","raw","raw","raw","raw","raw","raw","upper_bound","upper_bound"],"q":[[0,"lz"],[38,"lz::Subjects"],[40,"lz::dsa"],[57,"lz::dsa::Dsa"],[59,"lz::prealgebra"],[84,"lz::prealgebra::Prealgebra"],[107,"clap_builder::builder::command"],[108,"clap_builder::parser::matches::arg_matches"],[109,"clap_builder"],[110,"core::result"],[111,"clap_builder::util::id"],[112,"core::option"],[113,"core::any"]],"d":["","","","The subjects that can be used.","","","","","","","","","","","","Returns the argument unchanged.","Returns the argument unchanged.","","","","","","","Calls U::from(self).","Calls U::from(self).","","","","","","","","","","","","","","The function (command) to run.","","Returns true or false based on whether the vector has a …","","","","","","Returns the argument unchanged.","","","","Calls U::from(self).","","","","","","","The vector of numbers to detect whether it has a duplicate.","Whether or not to return the raw output.","Finds all factor pairs for a positive integer.","Finds all factors for a positive integer.","Determines if a positive integer is composite.","Determines if a positive integer is a factor of another …","Determines if a positive integer is a multiple of another …","Determines if a positive integer is prime.","Finds all multiples of a positive integer in a given range.","","Finds the prime factorization of a positive integer.","Finds all primes in a given range.","","","","","Returns the argument unchanged.","","","","Calls U::from(self).","","","","","","","The lower bound of the range to find multiples in.","The lower bound of the range to find primes in.","The positive integer to determine if it is a multiple.","The positive integer to determine if it is a factor.","The positive integer to find factor pairs for.","The positive integer to find factors for.","The positive integer to find multiples for.","The positive integer to find the prime factorization of.","The positive integer to determine if it is composite.","The positive integer to determine if it is prime.","The positive integer to determine if it is a factor.","The positive integer to determine if it is a multiple.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","The upper bound of the range to find multiples in.","The upper bound of the range to find primes in."],"i":[0,6,6,0,3,3,6,6,3,6,3,6,3,3,0,3,6,3,6,3,6,3,6,3,6,0,0,3,3,6,3,6,3,6,3,6,3,6,15,16,13,0,13,13,13,13,13,13,13,13,13,0,13,13,13,13,13,17,17,14,14,14,14,14,14,14,0,14,14,14,14,14,14,14,14,14,14,14,0,14,14,14,14,14,18,19,20,21,22,23,18,24,25,26,20,21,22,23,18,19,24,25,26,20,21,18,19],"f":[0,0,0,0,[1,1],[1,1],[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-2,[],[]],[[],1],[[],1],0,[-1,-1,[]],[-1,-1,[]],[2,[[5,[3,4]]]],[2,[[5,[6,4]]]],[2,[[5,[3,4]]]],[2,[[5,[6,4]]]],[[],[[8,[7]]]],[9,10],[-1,-2,[],[]],[-1,-2,[],[]],[[],11],0,0,[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[-1,12,[]],[[3,2],[[5,[11,4]]]],[[6,2],[[5,[11,4]]]],[[3,2],[[5,[11,4]]]],[[6,2],[[5,[11,4]]]],0,0,0,0,[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-1,[]],[2,[[5,[13,4]]]],[2,[[5,[13,4]]]],[9,10],[-1,-2,[],[]],[[[8,[13]]],11],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[[13,2],[[5,[11,4]]]],[[13,2],[[5,[11,4]]]],0,0,0,0,0,0,0,0,0,0,0,0,[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-1,[]],[2,[[5,[14,4]]]],[2,[[5,[14,4]]]],[9,10],[-1,-2,[],[]],[[[8,[14]]],11],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[[14,2],[[5,[11,4]]]],[[14,2],[[5,[11,4]]]],0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"c":[],"p":[[5,"Command",107],[5,"ArgMatches",108],[5,"Cli",0],[8,"Error",109],[6,"Result",110],[6,"Subjects",0],[5,"Id",111],[6,"Option",112],[1,"str"],[1,"bool"],[1,"tuple"],[5,"TypeId",113],[6,"Dsa",40],[6,"Prealgebra",59],[15,"Prealgebra",38],[15,"Dsa",38],[15,"ContainsDuplicate",57],[15,"MultiplesInRange",84],[15,"PrimesInRange",84],[15,"IsFactor",84],[15,"IsMultiple",84],[15,"FactorPairs",84],[15,"Factors",84],[15,"PrimeFactorization",84],[15,"IsComposite",84],[15,"IsPrime",84]],"b":[]}]\ +["ladderz",{"doc":"ladderz","t":"CCHHHHHHHHHHHH","n":["dsa","prealgebra","contains_duplicate","is_anagram","is_anagram2","get_factor_pairs","get_factors","get_multiples_in_range","get_prime_factorization","get_primes_in_range","is_composite","is_factor","is_multiple","is_prime"],"q":[[0,"ladderz"],[2,"ladderz::dsa"],[5,"ladderz::prealgebra"],[14,"alloc::vec"],[15,"alloc::string"],[16,"std::collections::hash::set"],[17,"std::collections::hash::map"]],"d":["Various data structures and algorithms implementations.","Various pre-algebra implementations including factor …","","","","Finds all factor pairs for a positive integer n.","Finds all factors of a positive integer n.","Finds all the multiples of a positive integer n starting …","Returns the prime factorization of a positive integer n.","Returns all prime numbers in the range [start, end].","Checks if a positive integer n is a composite number.","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …","Checks if a positive integer n is a prime number."],"i":[0,0,0,0,0,0,0,0,0,0,0,0,0,0],"f":[0,0,[[[2,[1]]],3],[[4,4],3],[[4,4],3],[5,[[7,[[6,[5,5]]]]]],[5,[[7,[5]]]],[[5,5,5],[[7,[5]]]],[5,[[8,[5,5]]]],[[5,5],[[7,[5]]]],[5,3],[[5,5],3],[[5,5],3],[5,3]],"c":[],"p":[[1,"i32"],[5,"Vec",14],[1,"bool"],[5,"String",15],[1,"u32"],[1,"tuple"],[5,"HashSet",16],[5,"HashMap",17]],"b":[]}],\ +["lz",{"doc":"lz","t":"FPPGNNNNNNNNNNCNNNNNNNNNNHCONNNNNNNNNNOOPGPNNNNNNNNNHNNNNNOOOOOPPPPPPPGPPNNNNNNNNNHNNNNNOOOOOOOOOOOOOOOOOOOOOOO","n":["Cli","Dsa","Prealgebra","Subjects","augment_args","augment_args_for_update","augment_subcommands","augment_subcommands_for_update","borrow","borrow","borrow_mut","borrow_mut","command","command_for_update","dsa","from","from","from_arg_matches","from_arg_matches","from_arg_matches_mut","from_arg_matches_mut","group_id","has_subcommand","into","into","main","prealgebra","subject","try_from","try_from","try_into","try_into","type_id","type_id","update_from_arg_matches","update_from_arg_matches","update_from_arg_matches_mut","update_from_arg_matches_mut","function","function","ContainsDuplicate","Dsa","IsAnagram","augment_subcommands","augment_subcommands_for_update","borrow","borrow_mut","from","from_arg_matches","from_arg_matches_mut","has_subcommand","into","match_dsa","try_from","try_into","type_id","update_from_arg_matches","update_from_arg_matches_mut","a","b","n","raw","raw","FactorPairs","Factors","IsComposite","IsFactor","IsMultiple","IsPrime","MultiplesInRange","Prealgebra","PrimeFactorization","PrimesInRange","augment_subcommands","augment_subcommands_for_update","borrow","borrow_mut","from","from_arg_matches","from_arg_matches_mut","has_subcommand","into","match_prealgebra","try_from","try_into","type_id","update_from_arg_matches","update_from_arg_matches_mut","lower_bound","lower_bound","m","m","n","n","n","n","n","n","n","n","raw","raw","raw","raw","raw","raw","raw","raw","raw","upper_bound","upper_bound"],"q":[[0,"lz"],[38,"lz::Subjects"],[40,"lz::dsa"],[58,"lz::dsa::Dsa"],[63,"lz::prealgebra"],[88,"lz::prealgebra::Prealgebra"],[111,"clap_builder::builder::command"],[112,"clap_builder::parser::matches::arg_matches"],[113,"clap_builder"],[114,"core::result"],[115,"clap_builder::util::id"],[116,"core::option"],[117,"core::any"]],"d":["","","","The subjects that can be used.","","","","","","","","","","","","Returns the argument unchanged.","Returns the argument unchanged.","","","","","","","Calls U::from(self).","Calls U::from(self).","","","","","","","","","","","","","","The function (command) to run.","","Returns true or false based on whether the vector has a …","","Returns true or false based on whether string a is an …","","","","","Returns the argument unchanged.","","","","Calls U::from(self).","","","","","","","The first string to compare against.","The second string to compare against.","The vector of numbers to detect whether it has a duplicate.","Whether or not to return the raw output.","Whether or not to return the raw output.","Finds all factor pairs for a positive integer.","Finds all factors for a positive integer.","Determines if a positive integer is composite.","Determines if a positive integer is a factor of another …","Determines if a positive integer is a multiple of another …","Determines if a positive integer is prime.","Finds all multiples of a positive integer in a given range.","","Finds the prime factorization of a positive integer.","Finds all primes in a given range.","","","","","Returns the argument unchanged.","","","","Calls U::from(self).","","","","","","","The lower bound of the range to find multiples in.","The lower bound of the range to find primes in.","The positive integer to determine if it is a multiple.","The positive integer to determine if it is a factor.","The positive integer to find factor pairs for.","The positive integer to find factors for.","The positive integer to find multiples for.","The positive integer to find the prime factorization of.","The positive integer to determine if it is composite.","The positive integer to determine if it is prime.","The positive integer to determine if it is a factor.","The positive integer to determine if it is a multiple.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","Whether or not to return the raw output.","The upper bound of the range to find multiples in.","The upper bound of the range to find primes in."],"i":[0,6,6,0,3,3,6,6,3,6,3,6,3,3,0,3,6,3,6,3,6,3,6,3,6,0,0,3,3,6,3,6,3,6,3,6,3,6,15,16,13,0,13,13,13,13,13,13,13,13,13,13,0,13,13,13,13,13,17,17,18,18,17,14,14,14,14,14,14,14,0,14,14,14,14,14,14,14,14,14,14,14,0,14,14,14,14,14,19,20,21,22,23,24,19,25,26,27,21,22,23,24,19,20,25,26,27,21,22,19,20],"f":[0,0,0,0,[1,1],[1,1],[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-2,[],[]],[[],1],[[],1],0,[-1,-1,[]],[-1,-1,[]],[2,[[5,[3,4]]]],[2,[[5,[6,4]]]],[2,[[5,[3,4]]]],[2,[[5,[6,4]]]],[[],[[8,[7]]]],[9,10],[-1,-2,[],[]],[-1,-2,[],[]],[[],11],0,0,[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[-1,12,[]],[[3,2],[[5,[11,4]]]],[[6,2],[[5,[11,4]]]],[[3,2],[[5,[11,4]]]],[[6,2],[[5,[11,4]]]],0,0,0,0,0,[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-1,[]],[2,[[5,[13,4]]]],[2,[[5,[13,4]]]],[9,10],[-1,-2,[],[]],[[[8,[13]]],11],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[[13,2],[[5,[11,4]]]],[[13,2],[[5,[11,4]]]],0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,[1,1],[1,1],[-1,-2,[],[]],[-1,-2,[],[]],[-1,-1,[]],[2,[[5,[14,4]]]],[2,[[5,[14,4]]]],[9,10],[-1,-2,[],[]],[[[8,[14]]],11],[-1,[[5,[-2]]],[],[]],[-1,[[5,[-2]]],[],[]],[-1,12,[]],[[14,2],[[5,[11,4]]]],[[14,2],[[5,[11,4]]]],0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"c":[],"p":[[5,"Command",111],[5,"ArgMatches",112],[5,"Cli",0],[8,"Error",113],[6,"Result",114],[6,"Subjects",0],[5,"Id",115],[6,"Option",116],[1,"str"],[1,"bool"],[1,"tuple"],[5,"TypeId",117],[6,"Dsa",40],[6,"Prealgebra",63],[15,"Prealgebra",38],[15,"Dsa",38],[15,"IsAnagram",58],[15,"ContainsDuplicate",58],[15,"MultiplesInRange",88],[15,"PrimesInRange",88],[15,"IsFactor",88],[15,"IsMultiple",88],[15,"FactorPairs",88],[15,"Factors",88],[15,"PrimeFactorization",88],[15,"IsComposite",88],[15,"IsPrime",88]],"b":[]}]\ ]')); if (typeof exports !== 'undefined') exports.searchIndex = searchIndex; else if (window.initSearch) window.initSearch(searchIndex); diff --git a/src/ladderz/dsa.rs.html b/src/ladderz/dsa.rs.html index f2cded7..6e98b67 100644 --- a/src/ladderz/dsa.rs.html +++ b/src/ladderz/dsa.rs.html @@ -23,7 +23,57 @@ 22 23 24 -

use std::collections::HashSet;
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+
use std::collections::{HashMap, HashSet};
 
 pub fn contains_duplicate(nums: Vec<i32>) -> bool {
     let mut seen = HashSet::<i32>::new();
@@ -36,6 +86,49 @@
     false
 }
 
+pub fn is_anagram(a: String, b: String) -> bool {
+    let mut letters = HashMap::new();
+    for c in a.chars() {
+        if let Some(&value) = letters.get(&c) {
+            letters.insert(c, value + 1);
+        } else {
+            letters.insert(c, 1);
+        }
+    }
+    for c in b.chars() {
+        if let Some(&value) = letters.get(&c) {
+            if value - 1 < 0 {
+                return false;
+            }
+            letters.insert(c, value - 1);
+        } else {
+            return false;
+        }
+    }
+    for (_, &count) in letters.iter() {
+        if count > 0 {
+            return false;
+        }
+    }
+    true
+}
+
+pub fn is_anagram2(a: String, b: String) -> bool {
+    if a.len() != b.len() {
+        return false;
+    }
+
+    let mut letters = HashMap::new();
+    for c in a.chars() {
+        *letters.entry(c).or_default() += 1;
+    }
+    for c in b.chars() {
+        *letters.entry(c).or_default() -= 1;
+    }
+
+    letters.into_values().all(|c: i32| c == 0)
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -46,5 +139,12 @@
         let expected = true;
         assert_eq!(result, expected);
     }
+
+    #[test]
+    fn test_is_anagram() {
+        let result = is_anagram("marc".to_owned(), "cram".to_owned());
+        let expected = true;
+        assert_eq!(result, expected);
+    }
 }
 
\ No newline at end of file diff --git a/src/lz/dsa.rs.html b/src/lz/dsa.rs.html index ed64e97..4b88112 100644 --- a/src/lz/dsa.rs.html +++ b/src/lz/dsa.rs.html @@ -56,6 +56,48 @@ 55 56 57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99
use clap::Subcommand;
 
 #[derive(Subcommand)]
@@ -92,6 +134,36 @@
         #[arg(short = 'r', long)]
         raw: bool,
     },
+    /// Returns true or false based on whether string a is an anagram of string b.
+    ///
+    /// ## Example
+    ///
+    /// ### Input
+    ///
+    /// ```bash
+    /// lz dsa is-anagram marc cram
+    /// ```
+    ///
+    /// ### Output
+    ///
+    /// ```bash
+    /// "marc" is an anagram of "cram".
+    /// ```
+    ///
+    /// ## Raw Output (use `-r` or `--raw`)
+    ///
+    /// ```bash
+    /// true
+    /// ```
+    IsAnagram {
+        /// The first string to compare against.
+        a: String,
+        /// The second string to compare against.
+        b: String,
+        /// Whether or not to return the raw output.
+        #[arg(short = 'r', long)]
+        raw: bool,
+    },
 }
 
 pub fn match_dsa(function: Option<Dsa>) {
@@ -108,6 +180,18 @@
                 )
             }
         },
+        Some(Dsa::IsAnagram { a, b, raw }) => match raw {
+            true => println!("{:?}", is_anagram(a, b)),
+            false => {
+                let result = is_anagram(a.clone(), b.clone());
+                println!(
+                    "{:?} {} an anagram of {:?}.",
+                    a,
+                    if result { "is" } else { "is not" },
+                    b,
+                )
+            }
+        },
         None => {
             println!("Please provide a function to use.");
         }