Advertisement
Kali_prasad

Lexio Swap

Dec 7th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. #https://www.codechef.com/problems/LEX
  2.  
  3. def getBlock(strn):
  4.     last = ''
  5.     block = []
  6.     '''
  7.    you are treating As and Bs as blocks
  8.    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
  9.    - even blocks can be made as small as 2 chars
  10.    - odd blocks can be made as small as 1 char
  11.    so this raises some simple conditions to check that s can be made as t or not
  12.    those are
  13.     - blocks orders should be matching for both sblock and tblock
  14.     - parity of those blocks should be matching
  15.     - sblock should be higher or equal to t block as sblock can be shrinked if required
  16.    '''
  17.     for ch in strn:
  18.         if last==ch:
  19.             block[-1][1]+=1
  20.         else:
  21.             last=ch
  22.             block.append([ch,1])
  23.     return block
  24.    
  25. def checkBlockOrdersAndVerify(sblock,tblock):
  26.     #we reach this point only when both block lengths matches
  27.     for index in range(len(sblock)):
  28.         if sblock[index][0] != tblock[index][0]:#order validation
  29.             return False
  30.         if (sblock[index][1]%2) != (tblock[index][1]%2) :#parity validation
  31.             return False
  32.         if (sblock[index][1]) < (tblock[index][1]) :#length validation
  33.             return False
  34.     return True
  35.    
  36. def checkIfPossibleToMakeT(s,t):
  37.     if 'A' not in s or 'A' not in t or 'B' not in s or 'B' not in t:
  38.         #A or B is not present in one string ,so they have to match definitely to print YES
  39.         if s==t:
  40.             print('YES')
  41.         else:
  42.             print('NO')
  43.         return
  44.     sblock = getBlock(s)
  45.     tblock = getBlock(t)
  46.     if len(sblock)!=len(tblock):
  47.         print('NO')
  48.         return
  49.     else:
  50.        
  51.         if(not checkBlockOrdersAndVerify(sblock,tblock)):
  52.             print('NO')
  53.             return
  54.         else:
  55.             print('YES')
  56.             return
  57.        
  58.    
  59.    
  60. Q = int(input())
  61. for _ in range(Q):
  62.     n,m = map(int,input().split())
  63.     s = input()
  64.     t = input()
  65.     checkIfPossibleToMakeT(s,t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement