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.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026