Problem · String

Minimum Steps Required

Learn this problem
HardBarclayOA

Problem statement

Given two strings str1 and str2 containing only 0s and 1s, there are steps to change str1 to str2:

  • Find a substring of str1 of length 2 and reverse it, resulting in str1' (where str1' != str1).
  • Find a substring of str1' of length 3, reverse it, resulting in str1'' (where str1'' != str1').
  • Repeat similar steps.
  • String length ranges from 2 to 30.

    Requirements: 🤩

  • Each step must be performed once, and you cannot skip previous steps to perform the next step.
  • If it's possible to change str1 to str2, output the minimum required steps. Otherwise, output -1.
  • Function

    minimumStepsRequired(str1: String, str2: String) → int

    Examples

    Example 1

    str1 = "1010"str2 = "0011"return = 2
    Steps:
  • Choose substring in range [2, 3]: "1010" → "1001"
  • Choose substring in the range [0, 2]: "1001" → "0011"
  • Example 2

    str1 = "1001"str2 = "0110"return = -1
    It's impossible to change str1 to str2.

    Example 3

    str1 = "10101010"str2 = "00101011"return = 7
    The minimum steps required to change str1 to str2 is 7.

    More Barclay problems

    drafts saved locally
    public int minimumStepsRequired(String str1, String str2) {
        // write your code here
    }
    
    str1"1010"
    str2"0011"
    expected2
    checking account