Problem · String

Check Similar Passwords (Also for Fungile)

MediumAmazonINTERNNEW GRADOA
See Amazon hiring insights

Amazon would like to enforce a password policy for password changes. For each pair of strings newPasswords[i] and oldPasswords[i], determine whether the two passwords are similar.

To test similarity, you may choose any set of indices in newPasswords[i], including the empty set. Each chosen character is changed exactly once to its next cyclic lowercase character: 'a' becomes 'b', 'b' becomes 'c', and 'z' becomes 'a'. Characters at indices not chosen remain unchanged.

The passwords are similar if, after applying the operation to the new password, oldPasswords[i] is a subsequence of the transformed new password.

A subsequence is a sequence that can be derived from another sequence by deleting zero or more characters without changing the order of the remaining characters.

Return an array of strings where the i-th result is "YES" if oldPasswords[i] can be made a subsequence of newPasswords[i] using the operation above, and "NO" otherwise.

Examples
01 · Example 1
newPasswords = ["baacbab", "accdb", "baacba"]
oldPasswords = ["abdbc", "ach", "abb"]
return = ["YES", "NO", "YES"]

For the first pair, choose indices 3, 4, and 7 in "baacbab" using 1-based indexing. The selected characters "a", "c", and "b" become "b", "d", and "c", so "abdbc" is a subsequence of the transformed password.

For the second pair, no choice of indices can make "ach" a subsequence of "accdb", because no transformed character can become 'h'.

For the third pair, "abb" can be matched as a subsequence after choosing an appropriate subset of indices.

02 · Example 2
newPasswords = ["aaccbbee", "aab"]
oldPasswords = ["bdbf", "aee"]
return = ["YES", "NO"]
Example 2 illustration

For newPasswords[0] = "aaccbbee" and oldPasswords[0] = "bdbf", choose characters so the transformed new password contains "bdbf" as a subsequence.

For newPasswords[1] = "aab" and oldPasswords[1] = "aee", there is no way to obtain two 'e' characters as a subsequence, so the answer is "NO".

Constraints
  • 1 <= n <= 10
  • newPasswords.length == oldPasswords.length == n
  • 1 <= oldPasswords[i].length <= newPasswords[i].length
  • The total length of all strings in newPasswords and oldPasswords does not exceed 2 * 105.
  • All passwords consist of lowercase English letters.
More Amazon problems
drafts saved locally
public String[] checkSimilarPasswords(String[] newPasswords, String[] oldPasswords) {
  // write your code here
}
newPasswords["baacbab", "accdb", "baacba"]
oldPasswords["abdbc", "ach", "abb"]
expected["YES", "NO", "YES"]
sign in to submit