in reply to Check if string A and B are made up of same chars
It's late and I haven't benchmarked it. ( I suppose it also depends on the length of the strings.)
For multiple strings you'll need to reset %h to the first count.
use strict; use warnings; sub test { my ($str1,$str2) = @_; my %h; $h{$_}++ for split //, $str1; $h{$_}-- for split //, $str2; $_ and return 0 for values %h; return 1; } print test(("Hello World!")x2); # 1 print test("Hello World!","Hello World"); # 0
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Check if string A and B are made up of same chars
by harangzsolt33 (Deacon) on Nov 17, 2024 at 01:57 UTC | |
by LanX (Saint) on Nov 17, 2024 at 12:55 UTC |