Is Substitution Cipher? (Codefight problem)


This is a codefight problem and thought of solving it in Javascript after coming in contact with caesar’s cipher: Would be glad to see your solutions.
link to it

A ciphertext alphabet is obtained from the plaintext alphabet by means of rearranging some characters. For example “bacdef…xyz” will be a simple ciphertext alphabet where a and b are rearranged.

A substitution cipher is a method of encoding where each letter of the plaintext alphabet is replaced with the corresponding (i.e. having the same index) letter of some ciphertext alphabet.

Given two strings, check whether it is possible to obtain them from each other using some (possibly, different) substitution ciphers.


    For string1 = "aacb" and string2 = "aabc", the output should be

isSubstitutionCipher(string1, string2) = true.

Any ciphertext alphabet that starts with acb… would make this transformation possible.

    For string1 = "aa" and string2 = "bc", the output should be

isSubstitutionCipher(string1, string2) = false.


[time limit] 4000ms (py3)
[input] string string1

A string consisting of lowercase English characters.

Guaranteed constraints:
1 ≤ string1.length ≤ 10.

[input] string string2

A string consisting of lowercase English characters of the same length as string1.

Guaranteed constraints:
string2.length = string1.length.

[output] boolean


What have you done so far to solve it?


I have a python code which is not perfect yet. Still fail some hidden tests.
This is it:

def isSubstitutionCipher(string1, string2):

    for i in range(len(string1)):
         if string1.count(string1[i]) != string2.count(string1[i]):
            if string1[i] in string2:
                return False
                return True
    return True


Do you need help porting your code to javascript? It’s probably best to take a step back before writing any javascript and discuss your approach to solving the problem - in everyday language not code

btw the link you provided does not work - gives an unauthorized error - maybe you can copy some test cases here