Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://www.codechef.com/problems/LEX
- def getBlock(strn):
- last = ''
- block = []
- '''
- you are treating As and Bs as blocks
- so if the s string is homogenous(contains A and B) you can make those blocks into smallest blocks if required as per the given operations
- - even blocks can be made as small as 2 chars
- - odd blocks can be made as small as 1 char
- so this raises some simple conditions to check that s can be made as t or not
- those are
- - blocks orders should be matching for both sblock and tblock
- - parity of those blocks should be matching
- - sblock should be higher or equal to t block as sblock can be shrinked if required
- '''
- for ch in strn:
- if last==ch:
- block[-1][1]+=1
- else:
- last=ch
- block.append([ch,1])
- return block
- def checkBlockOrdersAndVerify(sblock,tblock):
- #we reach this point only when both block lengths matches
- for index in range(len(sblock)):
- if sblock[index][0] != tblock[index][0]:#order validation
- return False
- if (sblock[index][1]%2) != (tblock[index][1]%2) :#parity validation
- return False
- if (sblock[index][1]) < (tblock[index][1]) :#length validation
- return False
- return True
- def checkIfPossibleToMakeT(s,t):
- if 'A' not in s or 'A' not in t or 'B' not in s or 'B' not in t:
- #A or B is not present in one string ,so they have to match definitely to print YES
- if s==t:
- print('YES')
- else:
- print('NO')
- return
- sblock = getBlock(s)
- tblock = getBlock(t)
- if len(sblock)!=len(tblock):
- print('NO')
- return
- else:
- if(not checkBlockOrdersAndVerify(sblock,tblock)):
- print('NO')
- return
- else:
- print('YES')
- return
- Q = int(input())
- for _ in range(Q):
- n,m = map(int,input().split())
- s = input()
- t = input()
- checkIfPossibleToMakeT(s,t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement