Find Anagram String - Programming Puzzles

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.

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

Solutions

Jump to C, JavaScript, PHP, C# Solution.
You can submit your own solution in comment section in same/different programming language. If your solution is better or in different language we'll add here.

C

#include <stdio.h>

t[256],i;
int main(c)
{
    for(;c+3;)
        (i=getchar())>10?t[i]+=c:(c-=2);
    for(i=257;--i&&!t[i-1];);
    puts(i?"false":"true");
    return 0;
}

JavaScript

<html>
<body>
    <script>
        function a(b){
            return b.split('').sort().join()
        }
        alert(a('abc')==a('bca'));
     </script>
</body>
</html>

PHP

function d($s){
    return array_count_values(str_split($s));
}

echo d($argv[1]) == d($argv[2]);

CSharp

using System;
using System.Linq;

namespace testcs
{
    class Program
    {
        static void Main(string[] args)
        {
            string a="asd", b="sda";
            Console.Write(a.OrderBy(_ => _).SequenceEqual(b.OrderBy(_ => _)));
        }
    }
}


You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.