85
\$\begingroup\$

Challenge

Given two strings, work out if they both have exactly the same characters in them.

Example

Input

word, wrdo

This returns true because they are the same but just scrambled.

Input

word, wwro

This returns false.

Input

boat, toba

This returns true

Rules

Here are the rules!

  • Assume input will be at least 1 char long, and no longer than 8 chars.
  • No special characters, only az
  • All inputs can be assumed to be lowercase

Test Cases

boat, boat = true
toab, boat = true
oabt, toab = true
a, aa = false
zzz, zzzzzzzz = false
zyyyzzzz, yyzzzzzy = true
sleepy, pyels = false
p,p = true
\$\endgroup\$
  • 10
    \$\begingroup\$ 9 answers in 13 views... wow! \$\endgroup\$ – Tom Gullen Mar 8 '11 at 16:44
  • \$\begingroup\$ @Tom, because everyone wanted to prove that your comment about using a 64-bit integer was pointing in the wrong direction :P \$\endgroup\$ – Peter Taylor Mar 8 '11 at 18:15
  • 5
    \$\begingroup\$ Title request: Cod Elf, Go! \$\endgroup\$ – user54200 Jul 9 '16 at 12:48
  • 5
    \$\begingroup\$ "Falcon Rage, go mad!" \$\endgroup\$ – Geobits Oct 6 '16 at 17:25
  • 7
    \$\begingroup\$ My name suggestion: "are they anagrams" → "manage the arrays" \$\endgroup\$ – Esolanging Fruit Oct 31 '17 at 4:32

133 Answers 133

0
\$\begingroup\$

Axiom, 45 bytes

f(a:String,b:String):Boolean==sort(a)=sort(b)
\$\endgroup\$
0
\$\begingroup\$

Julia, 41 bytes

g(a,b)=sort(collect(a))==sort(collect(b))
\$\endgroup\$
0
\$\begingroup\$

Python, 99 chars

f=lambda w,x,y:w==y if x==""else any([f(w+a,x.replace(a,"",1),y)for a in x]) 
g=lambda x,y:f("",x,y)

Try it Online!

Recursively finds all permutations of x and compares them to y.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 4 bytes

_ISM

Try it Online

Explanation

_ISM
  SMQ   Sort both (implicit) inputs.
_I      Check if the result is invariant under reversing.
\$\endgroup\$
0
\$\begingroup\$

Perl, 60 bytes

Perl one-liner

perl -lanF -e 'push@a,join"",sort@F;END{exit($a[0]ne$a[1])}'

Takes first two lines of STDIN and compares them. Exits 0 if they're anagrams, 1 otherwise.

$ echo -e "word\nwrdo" | \
> perl -lanF -e 'push@a,join"",sort@F;END{exit($a[0]ne$a[1])}' && echo anagram
anagram
$ echo -e "word\nwrro" | \
> perl -lanF -e 'push@a,join"",sort@F;END{exit($a[0]ne$a[1])}' && echo anagram
$
\$\endgroup\$
0
\$\begingroup\$

Perl, 26 bytes

Includes +1 for p (using -F and @F ends up with the same score)

Give the input strings as 2 lines on STDIN.

(echo word; echo wrdo) | perl -pE '$\=${join W,sort/./g}++}{'

This prints 0 if not an anagram or 1 if it is an anagram

(echo word; echo wrdo) | perl -pE '$_=${join W,sort/./g}++'

comes in at 24 bytes and prints 00 if not an anagram and 01 if it is. If these values are numbers they are in fact valid falsy and thruthy in perl, but as strings they are both thruthy. It's probably fairer to consider them as strings so this solution is invalid

So this is the same length (but without warnings) as Xcali's answer (after optimizing):

(echo word; echo wrdo) | perl -F -E '@{$.}=sort@F}{say@1~~@2'
\$\endgroup\$
0
\$\begingroup\$

Tcl, 82 bytes

proc A x\ y {expr {[string le $x]==[string le $y]&[lsort -u [split $x $y]]=="{}"}}

Try it online!


Tcl, 83 bytes

proc A x\ y {expr {[string le $x]==[string le $y]&&[lsort -u [split $x $y]]=="{}"}}

Try it online!

Tcl, 84 bytes

proc S s {lsort [split $s ""]}
proc A x\ y {set x [S $x]
set y [S $y]
expr {$x==$y}}

Try it online!

Previous version was failing for comparing 2 to 20, because after the lsort and join the comparison became 2==02, and == was doing a numerical comparison instead of a numerical one!


Tcl, 94 bytes

proc S s {join [lsort [split $s ""]] ""}
proc A x\ y {set x [S $x]
set y [S $y]
expr {$x==$y}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 4 bytes

ms~=

Try it online!

Explanation

m      explicit map
 s     sort(array)
  ~    dump array to stack
   =   pop(a, b), push(a==b)
\$\endgroup\$
0
\$\begingroup\$

Pepe, 58 bytes

REEREEEEeEERREEeREEEEeEeErRREEEEEeEEreerrEEREEeeReEErEereE

Try it online! Input is in the form a;b. Outputs 0 for truthy and none for falsy.

\$\endgroup\$
0
\$\begingroup\$

Pushy, 13 bytes

Assumes the two inputs are separated by a space.

K32-$v;.gFgx#

Try it online!

                 \ Implicit: input on stack as character codes
K32-             \ Subtract 32 from all, making SPACE = 0
    $            \ Until 0 (space) on top of stack:
     v;          \   Send last char to auxiliary stack
       .         \ Space has been reached, pop it.
                 \ Now each stack contains all the letters of one of the words.
        gFg      \ Sort both stacks
           x     \ Check their equality
            #    \ Print result (1 if true, 0 if false)
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 108 bytes

Without LINQ. Returns 1 for true, 0 for false.

(a,b)=>{int x=a.Length==b.Length?1:0;foreach(char c in a){try{b.Remove(b.IndexOf(c));}catch{x=0;}}return x;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 10 9 5 4 bytes

á øV

Try it

á øV     :Implicit input of strings U & V
á        :Permutations of U
  øV     :Contains V?
\$\endgroup\$
  • \$\begingroup\$ As of v2.0a0, ¬n eV¬n works with 7 bytes. \$\endgroup\$ – Bubbler May 21 '18 at 7:42
  • \$\begingroup\$ øVá \$\endgroup\$ – Oliver Feb 1 at 2:22
0
\$\begingroup\$

Lua 112

function q(a,b)s=table.sort;t=table.concat;a={a:byte(1,#a)}s(a);b={b:byte(1,#b)}s(b)return t(a,".")==t(b,".")end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.