in reply to Check if string A and B are made up of same chars

Variety delights:

#!/usr/bin/env perl use strict; use warnings; use Set::Scalar; use List::MoreUtils qw(uniq); use feature qw(say); my $string = qw(kizuaheli); my $s_1 = Set::Scalar->new(uniq(split//, $string)); my $s_2 = Set::Scalar->new(uniq(split//, $string)); say $s_1->is_equal($s_2); say $s_1 == $s_2; my $s_3 = Set::Scalar->new(uniq(split//, $string)); my $s_4 = Set::Scalar->new(uniq(split//, "")); say $s_3->is_equal($s_4); say $s_3 == $s_4;

Update: arunbear has pointed out that the use of uniq is superfluous here - see below.

Replies are listed 'Best First'.
Re^2: Check if string A and B are made up of same chars
by Arunbear (Prior) on Nov 18, 2024 at 11:24 UTC
    The uniq is not really needed there because Set::Scalar itself will discard any duplicates.

      Yes, thanks. I had to convince myself again - it's been a long time since I last used it.

      my $s_1 = Set::Scalar->new(split//, "abc"); my $s_2 = Set::Scalar->new(split//, "aaaaaabc"); dd $s_1; dd $s_2; __END__ do { my $a = bless({ elements => { a => "a", b => "b", c => "c" }, universe => bless({ elements => { a => "a", b => "b", c => "c" }, null => bless({ universe => 'fix' }, "Set::Scalar::N +ull"), universe => undef, }, "Set::Scalar::Universe"), }, "Set::Scalar"); $a->{universe}{null}{universe} = $a->{universe}; $a; } do { my $a = bless({ elements => { a => "a", b => "b", c => "c" }, universe => bless({ elements => { a => "a", b => "b", c => "c" }, null => bless({ universe => 'fix' }, "Set::Scalar::N +ull"), universe => undef, }, "Set::Scalar::Universe"), }, "Set::Scalar"); $a->{universe}{null}{universe} = $a->{universe}; $a; }
Re^2: Check if string A and B are made up of same chars
by LanX (Saint) on Nov 17, 2024 at 17:42 UTC
    Well the OP said "made up of same chars" but he wants "same amount of same characters" ¹

    Please compare how his test for "ABB" and "ABA" are supposed to fail.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    ¹) or in math lingo "is a permutation"

        > And aren't you neglecting the length in your example?

        Nope, different length will fail. (See also second test)

        But yes, I was only concentrating on the essential part, his "specs" are indeed a bit fuzzy.

        Anyway, there's no point in optimizing for speed if the problem-space is under-defined

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery