forked from Aravinthanucep/Python-program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisomorphism.py
58 lines (45 loc) · 1.55 KB
/
isomorphism.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Aravinthan
#
# Created: 11/02/2018
# Copyright: (c) Aravinthan 2018
# Licence: <your licence>
#-------------------------------------------------------------------------------
MAX_CHARS = 256
# This function returns true if str1 and str2 are isomorphic
def isomorphic(string1, string2):
m = len(string1)
n = len(string2)
# Length of both strings must be same for one to one
# corresponance
if m != n:
return "Not isomorphic."
# To mark visited characters in str2
marked = [False] * MAX_CHARS
# To store mapping of every character from str1 to
# that of str2. Initialize all entries of map as -1
map = [-1] * MAX_CHARS
# Process all characters one by one
for i in xrange(n):
# if current character of str1 is seen first
# time in it.
if map[ord(string1[i])] == -1:
# if current character of st2 is already
# seen, one to one mapping not possible
if marked[ord(string2[i])] == True:
return "Not isomorphic."
# Mark current character of str2 as visited
marked[ord(string2[i])] = True
# Store mapping of current characters
map[ord(string1[i])] = string2[i]
# If this is not first appearance of current
# character in str1, then check if previous
# appearance mapped to same character of str2
elif map[ord(string1[i])] != string2[i]:
return "Not isomorphic."
return True
# Driver program
print isomorphic("1232","2342")