# Determine whether strings are anagrams

## 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

• 9 answers in 13 views... wow! – Tom Gullen Mar 8 '11 at 16:44
• @Tom, because everyone wanted to prove that your comment about using a 64-bit integer was pointing in the wrong direction :P – Peter Taylor Mar 8 '11 at 18:15
• Title request: Cod Elf, Go! – user54200 Jul 9 '16 at 12:48
• "Falcon Rage, go mad!" – Geobits Oct 6 '16 at 17:25
• My name suggestion: "are they anagrams" → "manage the arrays" – Esolanging Fruit Oct 31 '17 at 4:32

# Axiom, 45 bytes

f(a:String,b:String):Boolean==sort(a)=sort(b)


# Julia, 41 bytes

g(a,b)=sort(collect(a))==sort(collect(b))


# 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)


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

# Pyth, 4 bytes

_ISM


Try it Online

### Explanation

_ISM
SMQ   Sort both (implicit) inputs.
_I      Check if the result is invariant under reversing.


## Perl, 60 bytes

Perl one-liner

perl -lanF -e 'push@a,join"",sort@F;END{exit($ane$a)}'


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($ane$a)}' && echo anagram anagram$ echo -e "word\nwrro" | \
> perl -lanF -e 'push@a,join"",sort@F;END{exit($ane$a)}' && echo anagram
$ # 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'


# 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!

# MathGolf, 4 bytes

ms~=


Try it online!

## Explanation

m      explicit map
s     sort(array)
~    dump array to stack
=   pop(a, b), push(a==b)


# Pepe, 58 bytes

REEREEEEeEERREEeREEEEeEeErRREEEEEeEEreerrEEREEeeReEErEereE


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

# 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)


# 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!

# Japt, 1095 4 bytes

á øV


Try it

á øV     :Implicit input of strings U & V
á        :Permutations of U
øV     :Contains V?


# 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