DEV Community

loading...

Discussion on: These problem-solving patterns will help you ace your next coding interview

Collapse
albert_hadacek profile image
Albert Hadacek Author

Well, you cant do that in other languages. I wanted a solution that people can "replicate" using a language of their choice ;)

Collapse
jonrandy profile image
Jon Randy • Edited

Yes, you almost certainly can. I think plenty, if not most other languages have the ability to split strings, sort arrays, and join arrays. These are fairly common operations. The syntax would be different, sure - but the logic is easily repeatable

Thread Thread
jonrandy profile image
Jon Randy • Edited

Here is the same technique in Python (array join not necessary as we can do array equality):

def is_anagram(str1, str2):
  return sorted(list(str1)) == sorted(list(str2))
Enter fullscreen mode Exit fullscreen mode
Thread Thread
jonrandy profile image
Jon Randy

And in PHP (again, no join needed):

function isAnagram($str1, $str2) {
   $s1 = str_split($str1);
   $s2 = str_split($str2);
   sort($s1);
   sort($s2);
   return $s1 == $s2;
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
jonrandy profile image
Jon Randy

And in Ruby:

def is_anagram(str1, str2)
  str1.split('').sort() == str2.split('').sort()
end
Enter fullscreen mode Exit fullscreen mode