Sam
BAN USERIn Python 2.7
pattern = [ 'a','b','b','a', 'a', 'c' ]
s = [ 'cat', 'dog', 'bulldog', 'cat', 'cat', 'mouse']
def encodeString(pattern,s):
# Find distinct symbols in pattern
distinctSymbols = set(pattern)
print "The distinct symbols in the pattern are : %s\n" % (distinctSymbols)
# Find first occurences of all distinct patterns
firstOccurence = {}
for symbol in distinctSymbols:
firstOccurence[symbol] = pattern.find(symbol)
# Find the mapping between pattern and string
mapping = {}
for i in firstOccurence.values():
mapping[pattern[i]] = s[i]
print mapping
# Constrcut a new list from the mapping
newList = []
for symbol in pattern:
newList.append(mapping[symbol])
print "\nThe newly constructed string : %s" % newList
print newList == s
return newList == s
if encodeString(''.join(pattern),s):
print "The given pattern %s matches the list of strings %s.\n" % (pattern,s)
else:
print "The given pattern %s does not match the list of strings %s.\n" % (pattern,s)
"""
Pseudocode
find first occurence of a,b,c
map this first occurence on s to get the coressponding values for a,b,c in s, build a hash table with that mapping
replace contents in s with their coressponding map
check if str(pattern) == str(s)
"""
Python 2.7 implementation
- Sam December 05, 2016