Isomorphic Words


public class IsomorphicWords { public int countPairs(String[] words) { // fill in code here } }

Problem Statement

Two words are called isomorphic if the letters in one word can be remapped to get the second word. Remapping a letter means replacing all occurrences of it with another letter. The ordering of the letters remains unchanged. No two letters may map to the same letter, but a letter may map to itself.

For example, the words "abca" and "zbxz" are isomorphic because we can map 'a' to 'z', 'b' to 'b' and 'c' to 'x'.

Given a String[] words, return how many (unordered) pairs of words are isomorphic.



    {"abca", "zbxz", "opqr"}
    Returns: 1
    "abca" and "zbxz" are isomorphic, but none of the two is isomorphic with "opqr".

    {"aa", "ab", "bb", "cc", "cd"}
    Returns: 4
    The four isomorphic pairs are {"aa", "bb"}, {"aa", "cc"}, {"bb", "cc"} and {"ab", "cd"}.

    { "cacccdaabc", "cdcccaddbc", "dcdddbccad", "bdbbbaddcb",
      "bdbcadbbdc", "abaadcbbda", "babcdabbac", "cacdbaccad",
      "dcddabccad", "cacccbaadb", "bbcdcbcbdd", "bcbadcbbca" }
    Returns: 13