human
Check if string follows order of characters defined by a pattern or not | Set 2 | Python3 program to check if characters of a string follow pattern defined by given pattern ..
Insert all characters of pattern in a hash set ,.
Build modified string ( string with characters only from pattern are taken ).
Remove more than one consecutive occurrences of pattern characters from modified string ..
After above modifications , the length of modified string must be same as pattern length.
And pattern characters must also be same as modified string characters.
Driver Code"
How can the above be solved in Python?
 ... Can you provide a code to check if string follows order of characters defined by a pattern or not and the other steps required?
assistant
#Here is code to check if string follows order of characters defined by a pattern or not and the other steps required.
 def followsPattern ( string , pattern ) :
	patternSet = set ( )
	for i in range ( len ( pattern ) ) :
		patternSet . add ( pattern [ i ] )
	modifiedString = string
	for i in range ( len ( string ) - 1 , - 1 , - 1 ) :
		if not modifiedString [ i ] in patternSet :
			modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ]
	for i in range ( len ( modifiedString ) - 1 , 0 , - 1 ) :
		if modifiedString [ i ] == modifiedString [ i - 1 ] :
			modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ]
	if len ( pattern ) != len ( modifiedString ) :
		return False
	for i in range ( len ( pattern ) ) :
		if pattern [ i ] != modifiedString [ i ] :
			return False
	return True
if __name__ == " _ _ main _ _ " :
	string = " engineers ▁ rock "
	pattern = " er "
	print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) )
	string = " engineers ▁ rock "
	pattern = " egr "
	print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) )
	string = " engineers ▁ rock "
	pattern = " gsr "
	print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) )
	string = " engineers ▁ rock "
	pattern = " eger "
	print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) )