Problem · Dynamic Programming

Calculate String Transformations Length

Learn this problem
HardBNY MellonINTERNOA

Problem statement

For a string word of length n consisting of lowercase English characters, it can be transformed as follows.

  • For each character of the string:

    1. If the character is 'z', it is replaced by "ab".

    2. Otherwise, it is replaced by its next higher character, for example, 'a' is replaced by 'b', 'b' by 'c' ... and 'y' by 'z'.

  • Thus, if the initial string is "azbk" it is "babcl" after 1 transformation.

    As a fun trivia, HackerRank publishes a puzzle for all its employees each week. The goal for this week is to find the length of the resultant string after exactly t transformations are performed as described above. Since the answer can be large, find the answer taking modulo (109 + 7).

    Function

    calculateTransformationsLength(word: String, t: int) → int

    Examples

    Example 1

    word = "abczy"t = 2return = 7

    Consider word = "abczy" and the number of transformations t = 2.

    In the 1st transformation, the characters are transformed as follows:

    • a → b
    • b → c
    • c → d
    • z → ab
    • y → z
    Resulting string: "bcdabz"

    In the 2nd transformation:

    • b → c
    • c → d
    • d → e
    • a → b
    • b → c
    • z → ab
    Resulting string: "cdebcab", whose length is 7.

    More BNY Mellon problems

    drafts saved locally
    public int calculateTransformationsLength(String word, int t) {
      // write your code here
    }
    
    word"abczy"
    t2
    expected7
    checking account