Problem · Dynamic Programming
Transform String (Google India)
Learn this problemProblem statement
Given two strings of equal length made up of 'x', 'y', and 'z', with no consecutive characters the same, determine the minimum number of operations needed to transform the first string into the second. In one operation, you can change any character in the first string, ensuring no consecutive characters become identical.
Function
transformString(str1: String, str2: String) → intExamples
Example 1
str1 = "zxyz"str2 = "zyxz"return = 6One shortest valid sequence is zxyz → yxyz → yzyz → yzxz → zxzx → zxyz → zyxz.
Example 2
str1 = "xyzyzyxyzx"str2 = "xzyzyzyxzy"return = 15The minimum number of valid one-character changes is 15.
Example 3
str1 = "xyxyxyxyxy"str2 = "xzyxyxzyxz"return = 13The minimum number of valid one-character changes is 13.
Example 4
str1 = "xyxyzyzyxy"str2 = "zyzyxzyzyz"return = 9The minimum number of valid one-character changes is 9.
Example 5
str1 = "xzxyxyzyzyxyzx"str2 = "zyzyxzyzyzyxzy"return = 20The minimum number of valid one-character changes is 20.
Constraints
str1.length == str2.length- Both strings are non-empty and contain only
'x','y', and'z'. - Neither input string contains two equal adjacent characters.