Untitled

mail@pastecode.io avatar
unknown
python
a year ago
742 B
1
Indexable
Never
def checkSimilarPasswords(newPasswords, oldPasswords):
  n = len(newPasswords)
  result = []
  for i in range(n):
    indices = []
    new_idx = 0
    for old_idx, char in enumerate(oldPasswords[i]):
      if char != newPasswords[i][new_idx]:
        indices.append(new_idx)
      new_idx = (new_idx + 1) % len(newPasswords[i])
    if isSubsequence(newPasswords[i], oldPasswords[i], indices):
      result.append("YES")
    else:
      result.append("NO")
  return result

def isSubsequence(newPassword, oldPassword, indices):
  j = 0
  for i, char in enumerate(oldPassword):
    if j == len(newPassword):
      return True
    if char == newPassword[j]:
      j += 1
    elif i in indices:
      j = (j + 1) % len(newPassword)
  return False