aboutsummaryrefslogtreecommitdiff
path: root/challenge-092/paulo-custodio/python/ch-1.py
blob: 70e334812ea2a2cd075710ff5428bc71509d5b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python

# Challenge 092
#
# TASK #1 > Isomorphic Strings
# Submitted by: Mohammad S Anwar
# You are given two strings $A and $B.
#
# Write a script to check if the given strings are Isomorphic. Print 1 if they
# are otherwise 0.
#
# Example 1:
# Input: $A = "abc"; $B = "xyz"
# Output: 1
# Example 2:
# Input: $A = "abb"; $B = "xyy"
# Output: 1
# Example 3:
# Input: $A = "sum"; $B = "add"
# Output: 0

import sys

def isomorphic(a,b):
    if len(a)!=len(b):
        return 0
    else:
        mapping = {}
        mapped = {}
        for i in range(0, len(a)):
            if not a[i] in mapping:         # a is new
                if b[i] in mapped:          # b already mapped to some other a
                    return 0
                else:                       # store mapping
                    mapping[a[i]] = b[i]
                    mapped[b[i]] = 1
            else:                           # a already occurred
                if mapping[a[i]]!=b[i]:     # previous mapping is different
                    return 0
        return 1

print(isomorphic(sys.argv[1], sys.argv[2]))