Two words are isomorphs if they have the same pattern of letter repetitions. For example, both ESTATE and DUELED have pattern abcdca

ESTATE
DUELED

abcdca

because letters 1 and 6 are the same, letters 3 and 5 are the same, and nothing further. This also means the words are related by a substitution cipher, here with the matching E <-> D, S <-> U, T <-> E, A <-> L.

Write code that takes two words and checks whether they are isomorphs. Fewest bytes wins.

Input: Two non-empty strings of capital letters A..Z. If you wish, you can take these as a collection of two strings or as a single string with a separator.

Output: A consistent Truthy value for pairs that are isomorphs, and a consistent Falsey value if they are not. Strings of different lengths are valid inputs that are never isomorphs.

Test cases:

True:

ESTATE DUELED
DUELED ESTATE
XXX YYY
CBAABC DEFFED
RAMBUNCTIOUSLY THERMODYNAMICS
DISCRIMINATIVE SIMPLIFICATION

False:

SEE SAW
ANTS PANTS
BANANA SERENE
BANANA SENSES
AB CC
XXY XYY
ABCBACCBA ABCBACCAB
ABAB CD

Feel free to add more test cases you find useful.

Leaderboard

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

var QUESTION_ID=50472;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),e.has_more?getAnswers():process()}})}function shouldHaveHeading(e){var a=!1,r=e.body_markdown.split("\n");try{a|=/^#/.test(e.body_markdown),a|=["-","="].indexOf(r[1][0])>-1,a&=LANGUAGE_REG.test(e.body_markdown)}catch(n){}return a}function shouldHaveScore(e){var a=!1;try{a|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(r){}return a}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading),answers.sort(function(e,a){var r=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0],n=+(a.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0];return r-n});var e={},a=1,r=null,n=1;answers.forEach(function(s){var t=s.body_markdown.split("\n")[0],o=jQuery("#answer-template").html(),l=(t.match(NUMBER_REG)[0],(t.match(SIZE_REG)||[0])[0]),c=t.match(LANGUAGE_REG)[1],i=getAuthorName(s);l!=r&&(n=a),r=l,++a,o=o.replace("{{PLACE}}",n+".").replace("{{NAME}}",i).replace("{{LANGUAGE}}",c).replace("{{SIZE}}",l).replace("{{LINK}}",s.share_link),o=jQuery(o),jQuery("#answers").append(o),e[c]=e[c]||{lang:c,user:i,size:l,link:s.share_link}});var s=[];for(var t in e)e.hasOwnProperty(t)&&s.push(e[t]);s.sort(function(e,a){return e.lang>a.lang?1:e.lang<a.lang?-1:0});for(var o=0;o<s.length;++o){var l=jQuery("#language-template").html(),t=s[o];l=l.replace("{{LANGUAGE}}",t.lang).replace("{{NAME}}",t.user).replace("{{SIZE}}",t.size).replace("{{LINK}}",t.link),l=jQuery(l),jQuery("#languages").append(l)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:&lt;(?:s&gt;[^&]*&lt;\/s&gt;|[^&]+&gt;)[^\d&]*)*$)/,NUMBER_REG=/\d+/,LANGUAGE_REG=/^#*\s*([^,]+)/;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table></div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table></div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table><table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table>

  • Are the lengths of the two inputs guaranteed to be same ? – Optimizer May 19 '15 at 21:04
  • @Optimizer No, the lengths can be different. – xnor May 19 '15 at 21:07
  • @Jakube No, your code should in theory work with inputs of any length. It's OK though if huge inputs fail on hardware due to issues like memory overflow or stack depth. – xnor May 20 '15 at 21:06
  • O.k. Then I'll delete my answer. – Jakube May 20 '15 at 21:07
  • Important test case: ABAB CD (for zip-like approaches) – Sp3000 May 21 '15 at 2:49

42 Answers 42

J, 4 bytes

-:&=

Usage

   'THERMODYNAMICS' (-:&=) 'RAMBUNCTIOUSLY'  NB. parens are optional
1 

Explanation

  • = with 1 argument creates an equality-table comparing the elements of the input and its nub.

    ='ESTATE' gives the binary matrix
    
    = | E S T A T E    
    --+------------
    E | 1 0 0 0 0 1
    S | 0 1 0 0 0 0
    T | 0 0 1 0 1 0
    A | 0 0 0 1 0 0
    
  • -: with 2 arguments checks their equality (like == generally does). This works for different size matrices (or even different types) too.

  • f&g applies g to both input separately and then applies f to the two results together so x f&g y == f(g(x), g(y)).

  • So in our case we compare the two equality-tables.

Try it online here.

  • 2
    An interesting and elegant approach. Without an equivalent to that &, the closest thing you could do in K would probably be ~/{x=/:x}', which is a good bit longer. – JohnE May 19 '15 at 22:02
  • 17
    Jesus. This has to be a contender for the codegolf hall of fame. – Brian Gordon May 21 '15 at 22:36
  • Wow, did not expect classify = to have any other use than for counting occurrences. – miles Jan 26 '17 at 19:46

K, 5 bytes

This has a delightfully elegant solution in K!

~/=:'

The "group" operator (monadic =) creates precisely the signature we want for word isomorphism; gathering vectors of the indices of each element of a vector, with the groups ordered by appearance:

  ="ABBAC"
(0 3
 1 2
 ,4)

  ="DCCDF"
(0 3
 1 2
 ,4)

Taking a pair of strings as a vector, we just need to apply group to each element (=:') and then reduce with "match" (~), the deep-equality operator:

  ~/=:'("RAMBUNCTIOUSLY";"THERMODYNAMICS")
1
  ~/=:'("BANANA";"SERENE")
0

Python 2, 41 bytes

f=lambda a,b:map(a.find,a)==map(b.find,b)
  • 4
    This was the solution that inspired me to create this challenge! – xnor May 22 '15 at 3:28

CJam, 9 bytes

r_f#r_f#=

Prints 1 if the words are isomorphs and 0 if they're not.

Try it online in the CJam interpreter.

How it works

r    e# Read a whitespace separated token from STDIN.
_    e# Push a copy.
f#   e# Get the indexes of all characters from the first copy in the second.
r_f# e# Repeat for the second word.
=    e# Check for equality.

JavaScript, ES7, 62 55 54 52 51 bytes

f=(x,y,g=z=>[for(i of z)z.search(i)]+0)=>g(x)==g(y)

The logic is simple. I simply convert both the inputs into their corresponding character index values, convert that array into string and compare.

f=(x, y,                  // Create a function named f which takes two arguments x and y
   g=                     // There is a third default argument to f which equals to
     z=>                  // and arrow function which takes argument z
     [                    // Return this array which is created using array comprehension
      for(i of z)         // For each character of z
      z.search(i)         // Use the index of that character in z in place of the character
     ]+0                  // And finally type cast that array to a string
                          // Here, the array elements are automatically joined by a ','
                          // and appended by a 0.
                          // Its funny how JS type casts Array + Number to a string
   )=>                    // Now the body of function f starts
      g(x)==g(y)          // It simply returns if index map of x equals index map of y

Try the above code using the snippet below.

f=(x,y,g=z=>[for(i of z)z.search(i)]+0)=>g(x)==g(y)

// for cross browser testing, here is a slightly modified ES5 version of above function

if (!f) {
  function f(x, y) {
    function g(z) {
      var Z = [];
      for (var i = 0; i < z.length; i++) {
        Z[i] = z.search(z[i])
      }
      return Z + 0;
    }
    return g(x) == g(y);
  }
}

B.onclick=function() {
  O.innerHTML = f(D.value, E.value);
  
};
<pre>f(<input id=D />,<input id=E />)</pre><button id=B >Run</button>
<br>
<output id=O />

2 bytes saved thanks to @edc65

  • 7
    +1, Tried it, works well. +0 instead of +""? – edc65 May 19 '15 at 21:28
  • 1
    @edc65 wow, typecasting WTF – Optimizer May 19 '15 at 21:29
  • 1
    I realized just now that the strings are 'A-Z', so you can safely use search instead of indexOf and cut 1 more byte. – edc65 May 20 '15 at 7:57
  • array comprehensions havent been cut of es7 eventually? where does this code works? i think only in mozilla – DanielIndie Oct 1 '17 at 15:18

Bash + coreutils, 38

[ `tr $@<<<$1``tr $2 $1<<<$2` = $2$1 ]

Note we are using the usual shell idea of truthy/falsy here - zero means SUCCESS or TRUE and non-zero means error or FALSE:

$ for t in "ESTATE DUELED" "DUELED ESTATE" "XXX YYY" "CBAABC DEFFED" "RAMBUNCTIOUSLY THERMODYNAMICS" "DISCRIMINATIVE SIMPLIFICATION" "SEE SAW" "ANTS PANTS" "BANANA SERENE" "BANANA SENSES" "AB CC" "XXY XYY" "ABCBACCBA ABCBACCAB"; do
> ./isomorph.sh $t
> echo $t $?
> done
ESTATE DUELED 0
DUELED ESTATE 0
XXX YYY 0
CBAABC DEFFED 0
RAMBUNCTIOUSLY THERMODYNAMICS 0
DISCRIMINATIVE SIMPLIFICATION 0
SEE SAW 1
ANTS PANTS 1
BANANA SERENE 1
BANANA SENSES 1
AB CC 1
XXY XYY 1
ABCBACCBA ABCBACCAB 1
$ 

Haskell, 33 29

EDIT:

this is way too late, but i found this improvement using applicatives, that were added to prelude only in march 2015.

s%k=g s==g k
g s=(==)<$>s<*>s

Old version:

s%k=g s==g k
g s=[a==b|a<-s,b<-s]

the checking function is (%)

this works by generating for each string its "equality record": for each two indices i j, it records whether they have equal characters. the record is ordered so that the record for two indices i, j is always in the same place* and therefore checking the equality of the records would return whether or not the strings have the same pattern.

for example, the equality record of "ABC" is [1,0,0,0,1,0,0,0,1] (1 for true, 0 for false) - there is True where any index is compared with itself. anywhere else is a false. (skipping these checks might be more efficient, but is harder in terms of golfng)

*if the strings are of the same length. otherwise it returns false just because the records are of different lengths

Haskell, 45 41 bytes

h l=map(`lookup`zip l[1..])l
x!y=h x==h y

Returns True or False, e.g "ESTATE" ! "DUELED" -> True.

Uses the map-char-to-first-index method as seen in many other answers. Association lists come in handy, because earlier entries trump. "aba" becomes [(a,1),(b,2),(a,3)] where lookup always fetches a -> 1.

Edit: @Mauris found 4 bytes to save.

  • You can replace (flip lookup$zip l[1..]) by (`lookup`zip l[1..]). – Lynn May 23 '15 at 1:49

Brainfuck, 169 168 162 144 140 131 130

Compatible with Alex Pankratov's bff (brainfuck interpreter used on SPOJ and ideone) and Thomas Cort's BFI (used on Anarchy Golf).

The expected input is two strings separated by a tab, with no newline after the second string. The output is 1 for isomorphs and 0 for non-isomorphs, which is convenient for checking results visually, although not the shortest option. (Update: shorter version with \x01 and \x00 as output and \x00 as separator at the bottom of the answer.)

Demonstration on ideone.

,+
[
  -
  ---------
  >+<
  [
    >>-<
    [
      <
      [
        >+<
        <<<<-<+>>>>>-
      ]
      ++[->+]
      ->+[+<-]
      >[<<<<]
      <
    ]
    <[>+<-]
    +[->+]
    <->
    >>>
  ]
  >
  [
    [[-]<<<<<]
    >>>>
  ]
  <,+
]
>>>+>+
[
  [<->-]
  <[>>>>>]
  <<<<
]
-<[>]
+++++++[<+++++++>-]
<.

This problem turns out to be very nice for brainfuck.

The basic idea with indexing is to go backwards from the end of the current string prefix. If the character has not previously occurred, we can take the length of the string prefix. For example:

STATES
123255

The indexing in the code is actually slightly different but uses the same principle.

The memory layout is in blocks of 5:

0 0 0 0 0 0 c 0 i p 0 c 0 i p 0 c 0 i p 0 0 0 0

c stands for character, i for index, and p for previous (index). When the first string is being processed, all the p slots are zero. The cell to the left of c is used to hold a copy of the current character that we are trying to find the index of. The cell to the left of the current i is used to hold a -1 for easy pointer navigation.

There are many conditions that need to be considered carefully. At the end, we check for isomorphs by comparing the (i,p) pairs, and we reach the cluster of zero cells to the left of the leftmost (i,p) pair if and only if the strings are isomorphs. Here is a commented version of the code to make it easier to follow:

,+
[                       while there is input
  -
  ---------
  >+<                   increment char (adjust later)
  [                     if not tab
    >>-<                set navigation flag
    [                   loop to find index
      <                 travel to copy
      [
        >+<             restore char
        <<<<-<+>>>>>-   compare chars and create copy
      ]
      ++[->+]           travel between navigation flags
      ->+[+<-]          increment index by 2 and go back
      >[<<<<]           proceed if not fallen off string
      <                 compare chars
    ]
    <[>+<-]             restore char (or no op)
    +[->+]              go back to navigation flag
    <->                 adjust char
    >>>                 alignment
  ]
  >
  [                     if tab
    [[-]<<<<<]          erase chars and go to beginning
    >>>>                alignment
  ]
  <,+
]
>>>+>+                  check string lengths and start loop
[
  [<->-]                compare indices
  <[>>>>>]              realign if not equal
  <<<<                  proceed
]
-<[>]                   cell to left is zero iff isomorphs
+++++++[<+++++++>-]
<.

Update:

Here is a version that prints \x01 for isomorphs and \x00 for non-isomorphs. This is arguably a more accurate interpretation of Truthy and Falsey for brainfuck, because of the way [ and ] work. The only difference is at the very end.

Additional: Now using \x00 as a separator to save 10 bytes.

+
[
  -
  >+<
  [
    >>-<
    [
      <
      [
        >+<
        <<<<-<+>>>>>-
      ]
      ++[->+]
      ->+[+<-]
      >[<<<<]
      <
    ]
    <[>+<-]
    +[->+]
    <->
    >>>
  ]
  >
  [
    [[-]<<<<<]
    >>>>
  ]
  <,+
]
>>>+>+
[
  [<->-]
  <[>>>>>]
  <<<<
]
-<[>]
<+.

JavaScript (ES6), 62

Using an aux function h that maps each word to an array containing the position of each letter in the word, for instance: PASS -> [1,2,3,3]. Return true if the function h applied the two words gives the same result.

f=(a,b,h=w=>0+[for(c of(n=k=[],w))k[c]=k[c]||++n])=>h(b)==h(a)

// TEST

;[
// True
 ['ESTATE','DUELED']
,['DUELED','ESTATE']
,['XXX','YYY']
,['CBAABC','DEFFED']
,['RAMBUNCTIOUSLY','THERMODYNAMICS']
,['DISCRIMINATIVE','SIMPLIFICATION']

// False:

,['SEE','SAW']
,['ANTS','PANTS']
,['BANANA','SERENE']
,['BANANA','SENSES']
,['XXY','XYY']
,['ABCBACCBA','ABCBACCAB']
]
.forEach(t=>(f(t[0],t[1])?OK:KO).innerHTML+=t+'\n')
Ok<br>
<pre id=OK></pre><br>
KO<br>
<pre id=KO></pre>

  • 1
    Sometimes, simple is shorter ;) – Optimizer May 19 '15 at 21:23

R, 78

function(x,y)identical((g=function(z)match(a<-strsplit(z,"")[[1]],a))(x),g(y))

De-golfed:

word_to_num <- function(word) {
   chars <- strsplit(word,"")[[1]]
   match(chars, chars)
}
are_isomorph <- function(word1, word2) identical(word_to_num(word1), 
                                                 word_to_num(word2))
  • beat me to it! (+1) – shadowtalker May 20 '15 at 4:20
  • I think all( (g=...)(x)==g(y)) is shorter than identical... – Giuseppe Sep 27 '17 at 19:01

Ruby, 83 bytes

t=->x{y=0;z=?`;x.gsub!(y[0],z.succ!)while y=x.match(/[A-Z]/);x};f=->a,b{t[a]==t[b]}

It's a function f that takes two arguments and returns true or false.

Explanation:

test = -> str {
    y = nil  # we're just initializing this; it doesn't matter to what
             # this is the variable we use to store the `match' result
    z = '`'  # backtick is the ASCII character before `a'
    while y = str.match(/[A-Z]/) do  # while there is an uppercase letter in str
        str.gsub!(y[0], z.succ!)  # replace all instances of the uppercase letter
                                  # with the next unused lowercase letter
    end
    str  # return the new string
}
# self-explanatory
f=->a,b{test[a]==test[b]}
  • 1
    This should save 4 bytes: t=->x{z=?`;x.chars.to_a.uniq.map{|c|x.gsub!(c,z.succ!)};x};f=->a,b{t[a]==t[b]}, and you could get it down to 68 if you use a hash for getting the replacement: t=->x{h={};i=9;x.gsub!(/./){|c|h[c]||h[c]=i+=1}};f=->a,b{t[a]==t[b]} – blutorange May 20 '15 at 7:19

Java, 107

(s,t)->java.util.Arrays.equals(s.chars().map(s::indexOf).toArray(),t.chars().map(t::indexOf).toArray())

Maps each character of s and t to its location, and checks for equality.

Expanded:

class Isomorphs {
    public static void main(String[] args) {
        java.util.function.BiFunction<String, String, Boolean> f =
            (s, t) -> java.util.Arrays.equals(
                                              s.chars().map(s::indexOf).toArray(),
                                              t.chars().map(t::indexOf).toArray()
                                             )
           ;
        System.out.println(f.apply("XXY", "XYY"));
    }
}
  • I don't think this will work correctly if the strings have different lengths. – JohnE May 20 '15 at 1:59
  • @JohnE Yes, it does. – Ypnypn May 20 '15 at 3:17
  • Ah, ok- I think the "expanded" version is misleading. – JohnE May 20 '15 at 3:23

Python 3, 85 bytes

f=lambda a,b:''.join(map(lambda g:dict(zip(a,b))[g],a))==b
g=lambda a,b:f(a,b)&f(b,a)
  • Where is the input/output on this one? – DJMcMayhem May 20 '15 at 4:11
  • @DJMcMayhem g is the main function, f is helper. There's a confusing choice of variable g inside f, but it's an unrelated bound variable.. The g= is optional as per the ruling allowing anon functions, which saves two chars.' – xnor May 20 '15 at 5:20

Pyth, 9 bytes

qFmmxdkdQ

Takes input in the following form:

"ESTATE", "DUELED"

If that is not acceptable, the following code is 10 bytes:

qFmmxdkd.z

and uses this input form:

ESTATE
DUELED

Uses the index of char in string representation.

  • The first input format is fine. I'm interested in how you're reducing to check equality but I'm unclear on how F works as fold. What's <binary>F? – xnor May 20 '15 at 21:15
  • @xnor <binary>F<seq> is <binary> folded over <seq>. It's equivalent to interspersing <binary> between every pair of elements of <seq>. Thus, <binary>F on a 2 element sequence simply applies the function to the sequence, equivalent to .* in Pyth or * in Python. – isaacg May 21 '15 at 7:00
  • I thought the trailing Q was implicit in Pyth? – Cyoce Nov 20 '16 at 23:36
  • @Cyoce Not back then - that feature was added in April 2016, almost a year later. – isaacg Nov 21 '16 at 3:54

Matlab, 50 bytes

f=@(s,t)isequal(bsxfun(@eq,s,s'),bsxfun(@eq,t,t'))

The function is defined as anonymous to save some space.

Example:

>> f=@(s,t)isequal(bsxfun(@eq,s,s'),bsxfun(@eq,t,t'));
>> f('ESTATE','DUELED')
ans =
     1
>> f('ANTS','PANTS')
ans =
     0

Octave, 26 bytes

@(s,t)isequal(s==s',t==t')
  • 3
    Looks interesting. explanation? – proud haskeller May 23 '15 at 18:19
  • == is matrix element-wise equality, and since s and s' are different sizes, octave's "broadcasting" automatically tries to get matrices of the same size to operate on -- which in this case means repeating the row s and column s' – rakslice Jun 22 '15 at 8:20
  • It's the same approach as @LuisMendo's Matlab solution, but there the expansion is explicit. – rakslice Jun 22 '15 at 8:28

05AB1E, 6 bytes

εæδË}Ë

Try it online!

Takes input as a list: ['ESTATE', 'DUELED']

Explanations:

    εæδË}Ë   Full program
    ε        Apply on each
     æ         Powerset
      δË       For each generated substring: 1 if all equal, 0 otherwise
        }    End for each
         Ë   1 if all equal, 0 otherwise

APL (Dyalog), 5 4 bytes

-1 thanks to ngn's hint.

Anonymous tacit prefix function which takes a list of two strings as argument.

≡.⍳⍨

Try it Online!

This is an inner product, but instead of the usual + and × it uses

 identicalness

. and

 the ɩndex (the first occurrence of each element)

 with the entire two-element list of words used as both arguments

If we call the words A and B, then we can derive the previous solution as follows:

≡.⍳⍨ A B
A B ≡.⍳ A B
(A⍳A) ≡ (B⍳B)
(⍳⍨A) ≡ (⍳⍨B)
≡/ ⍳⍨¨ A B

Previous solution

Anonymous tacit prefix function which takes a list of two strings as argument.

≡/⍳⍨¨

Try it online!

 identicalness

/ across

 the ɩndex (the first occurrence of each element…)

 selfie (…in itself)

¨ of each

  • can you see the inner product? :) – ngn Sep 30 '17 at 13:11
  • @ngn Yes of course. SIlly me. – Adám Sep 30 '17 at 20:12
  • Is the top link supposed to link to the old solution? – Zacharý Oct 5 '17 at 17:54
  • Too bad this doesn't work on higher rank arrays though :P – Zacharý Oct 8 '17 at 18:01
  • 1
    @Zacharý as promised: ngn.github.io/apl-codegolf-2017/readme.txt – ngn Oct 24 '17 at 9:24

Mathematica, 46 bytes

Equal@@Values@*PositionIndex/@Characters@{##}&

Ruby, 50 bytes

30 bytes shorter ruby code. Written before I took a look at the solutions, checks for each character of both strings whether the index of that character's first occurence matches; ie. transforms a string to its normalized form 01121 etc and compares those.

->x,y{g=->z{z.chars.map{|c|z=~/#{c}/}};g[x]==g[y]}

Test cases on ideone As an additional bonus, this breaks ideone's code highlighting.

Husk, 5 bytes

¤=´×=

Try it online!

Explanation

       -- implicit input A, B (strings aka character lists)       | "ab" "12"
¤=     -- apply the following function to A & B, then compare:    | [1,0,0,1] == [1,0,0,1] -> 1
  ´×   --   Cartesian product with itself under                   | ["aa","ba","ab","bb"] ["11","21","12","22"]
    =  --   equality                                              | [ 1  , 0  , 0  , 1  ] [ 1  , 0  , 0  , 1  ]

Ruby, 31 bytes

->a{!!a.uniq!{|s|s.tr s,'a-z'}}

A Proc that takes an array of strings and checks whether any are isomorphic to each other. tr s,'a-z' with these arguments normalizes a string s by replacing each letter with the nth letter in the alphabet, where n is the greatest index with which that letter appears in the string. For example, estate becomes fbedef, as does dueled.

PCRE, 84 bytes

^((.)(?=.+ (\3.|)(.))(?=((?=(\2|)?+.* \3\4(\7?(?(?=.*+\6)(?!\4).|\4))).)+ ))+. \3..$ 

Subject should be two space-separated words, as in the OP. Here's a cursory explanation:

For each letter X in the first word:

Look ahead to the second word and establish back references to recall how far along we are as well as the letter Y in the second word corresponding to X.

For each letter Z past the current position in the first word:

Establish similar back references as above.

Look ahead to the corresponding letter in the second word and check if Z = X then match a Y, otherwise match a letter that isn't Y.

This iteration can end once we've matched up until the penultimate letter in the first word. At this point, since no further validation is necessary, all that remains is to test that the words are of equal length (the back reference containing accumulating substrings of the second word is always behind by 1 letter).

Cobra, 72 bytes

do(a='',b='')=(for i in a get a.indexOf(i))==for i in b get b.indexOf(i)
  • Are you sure this marks the AB CC test case False? – xnor May 21 '15 at 6:26
  • @xnor fixed now – Οurous May 21 '15 at 6:49

JavaScript (ES5), 142 98

Quite a big one, but I didn't saw an ES5 version yet.

for(l=j=2;j--;){c=prompt();for(i=c.length;i--;)c=c.replace(RegExp(c[i],"g"),i);b=l==c;l=c}alert(b)

Simply replaces every occurence of the first letter with it's reverse index value. Repeats this for every character.

It does the same for both inputs and compares the generated pattern.

The comparison is quite ugly, but i din't wan't to use an array to store and compare it.

  • 1
    Could you not move ;l=c to for(l=j=2;j--; and save a byte? – Jonathan Frech Dec 8 at 15:07

Perl, 38 bytes

($_,$a)=@ARGV;eval"y/$_/$a/";say$_~~$a

Run as perl -E '($_,$a)=@ARGV;eval"y/$_/$a/";say$_~~$a' RAMBUNCTIOUSLY THERMODYNAMICS

Prints 1 if true, nothing if false.

Common Lisp, 76 bytes

(lambda(a b)(equal #1=(map'list(lambda(x)(position x a))a)(setf a b a #1#)))

Try it online!

C++, 213 196 162 bytes

-51 bytes thanks to Zacharý

#include<map>
#define F(X,x)for(auto&e:X){if(x.end()==x.find(e))x[e]=65+x.size();e=x[e];}
auto i=[](auto a,auto b){std::map<int,int>c,d;F(a,c)F(b,d)return a==b;};

To call the lambda, you need to pass 2 arguments that are std::string data type

Code to test :

std::initializer_list<std::pair<std::string, std::string>> test{
    {"ESTATE","DUELED"},
    {"DUELED","ESTATE"},
    {"XXX","YYY"},
    {"CBAABC","DEFFED"},
    {"RAMBUNCTIOUSLY","THERMODYNAMICS"},
    {"DISCRIMINATIVE","SIMPLIFICATION"},
    {"SEE","SAW"},
    {"ANTS","PANTS"},
    {"BANANA","SERENE"},
    {"BANAnA","SENSES"},
    {"AB","CC"},
    {"XXY","XYY"},
    {"ABCBACCBA","ABCBACCAB"},
    {"ABAB","AC"}
};

for (const auto& a : test) {
    std::cout << "Test with " << a.first << " and " << a.second <<
        " outputs : " << (i(a.first, a.second)?"TRUE":"FALSE") << '\n';
}

for the code that tests, including iostream and string header file is required

  • 1
    It doesn't look like you use anything from the string header, so can you remove it and have the user include it themselves? – Zacharý Oct 1 '17 at 14:16
  • Does this work for 161 bytes? – Zacharý Oct 5 '17 at 14:43
  • @Zacharý If you add the e as argument of find, yes, it works – HatsuPointerKun Oct 5 '17 at 16:12
  • That moment when you get beat by Brainfuck though >_< – Zacharý Oct 14 '17 at 16:52

JavaScript (ES6), 52 51 50 bytes

This version doesn't use array comprehension, and takes input using currying syntax.

a=>b=>(f=x=>0+[...x].map(c=>x.search(c)))(a)==f(b)

const isomorph = a=>b=>(f=x=>0+[...x].map(c=>x.search(c)))(a)==f(b);
const tests = [
  {input: ["ESTATE", "DUELED"], expected: true},
  {input: ["DUELED", "ESTATE"], expected: true},
  {input: ["XXX", "YYY"], expected: true},
  {input: ["CBAABC", "DEFFED"], expected: true},
  {input: ["RAMBUNCTIOUSLY", "THERMODYNAMICS"], expected: true},
  {input: ["DISCRIMINATIVE", "SIMPLIFICATION"], expected: true},
  {input: ["SEE", "SAW"], expected: false},
  {input: ["ANTS", "PANTS"], expected: false},
  {input: ["BANANA", "SERENE"], expected: false},
  {input: ["BANANA", "SENSES"], expected: false},
  {input: ["AB", "CC"], expected: false},
  {input: ["XXY", "XYY"], expected: false},
  {input: ["ABCBACCBA", "ABCBACCAB"], expected: false},
  {input: ["ABAB", "CD"], expected: false}
];

for(const { input: [str1, str2], expected } of tests) {
  console.log(`isomorph("${str1}")("${str2}") = ${isomorph(str1)(str2)}, expected = ${expected}`);
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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