Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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 A-Z
  • 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

Good luck!

The shorter an answer is, the better!

Edit

Shame this question is marked community wiki... I thought that was the purpose of the site?!?!

enter image description here

share|improve this question
2  
9 answers in 13 views... wow! – Tom Gullen Mar 8 '11 at 16:44
8  
ARGH! Why is this question marked community wiki? – Tom Gullen Mar 9 '11 at 10:39
14  
@Martin, part of the fun of this site for me is the competition in collecting rep! – Tom Gullen Mar 9 '11 at 11:27
2  
@TomGullen As to “why”, you can see it in the post's history: “more than 60 answers”. Perhaps you should propose on Meta that that automatic rule should be removed for this site. – Kevin Reid Jun 2 '12 at 14:43
show 3 more comments

63 Answers

1 2 3

R, 91

f=function(x,y) ifelse(identical(sort(strsplit(x,"")[[1]]),sort(strsplit(y,"")[[1]])),T,F)

Sample output:

f("boat","boat")
[1] TRUE
f("toab","boat")
[1] TRUE
f("oabt","toab")
[1] TRUE
f("a","aa")
[1] FALSE
f("zzz","zzzzzzzz")
[1] FALSE
f("zyyyzzzz","yyzzzzzy")
[1] TRUE
f("sleepy","pyels")
[1] FALSE
f("p","p")
[1] TRUE
share|improve this answer

CoffeeScript 129

Longer than the other CoffeeScript entry, but this one uses recursive string comparison, rather than just comparing sorted strings:

z=(x,y)->d=y.length;e=x.length;return 1if(!d&&!e);b=y.indexOf x[0];return 0if b<0;f=x[1..e];g=y[b+1..d];g=y[0..b-1]+g if b;z(f,g)

Outputs 1 or 0 indicating whether the strings are anagrams or not.

share|improve this answer

Javascript 70 (with primitive GUI)

Here's a Javascript entry that also includes a primitive GUI via two prompts and an alert.

function a(){return prompt('').split('').sort().join()}alert(a()==a())

Have a play – http://jsfiddle.net/liamnewmarch/jGues/

share|improve this answer
1 2 3

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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