FastPrepFastPrep
Problem Brief

String Challenge (Generic)

NEW GRADOA

At the TikTok Academy, students are learning how to improve video captions.

You are given a string caption that only has small Englsh letters ('a' to 'z'). You can make two kinds of changes to caption at any letter as many times as needed.

  • Change the letter to the one right before it in the alphabet. You cannot change 'a' because it is already the first letter 🥲
  • Change the letter to the one right after it in alphabet. You cannot change 'z' as it is already the last letter 🥹
  • For example, you can change 'f' to 'e' (the letter before) or 'g' (the letter after).

    What is your task this time? They want you to make that every letter in the caption has at least one same letter next to it. For example, "aaabb" and "aaccdd" are goooood, but "abaaa" and "abcdef" are veryyy bad 😭

    Plz return the min num of changes needed to make the caption meets the rules mentioned above.

    Thanks to Da Best Spike!⸜(。˃ ᵕ ˂ )⸝♡

    1Example 1

    Input
    caption = "aca"
    Output
    2
    Explanation
    You can change 'c' to 'b' (the letter before 'c'), then 'b' to 'a' (the letter before 'b'). Now the caption is 'aaa', which meets the rule. (This line of text might be slightly diff from the original explanation, but it should do well at capturing the meaning of the original explanation :) You will need 2 changes to meet the rule, which is min possible. So the ans is 2 :)

    2Example 2

    Input
    caption = "abcdef"
    Output
    3
    Explanation
    You can change caption[0] from 'a' to 'b' (the letter after :) cpation[3] from 'd' to 'c' (the letter before) and caption[4] from 'e' to 'f' (the letter after) now the caption is "bbccff", which meets the rule. In total, you'd need 3 chagnes to meet the rule, which is minimum possible. So, the answer is 3. (I think 2 is the typo on the image source)

    3Example 3

    Input
    caption = "abab"
    Output
    2
    Explanation
    You can change caption[0] from 'a' to 'b' (the letter after) change caption[3] from 'b' to 'a' (the letter before :) Now the caption is "bbaa", which meets the rule. In total, you need 2 changes to meet the rule, which is the min possible. So, return 2 as answer :P

    Constraints

    Limits and guarantees your solution can rely on.

    2 <= |caption| <= 105
    public int stringChallenge(String caption) {
      // write your code here
    }
    
    Input

    caption

    "aca"

    Output

    2

    Sign in to submit your solution.