Shift the letters
Practice
3.7 (30 votes)
Algorithms
Dynamic programming
Easy
Two dimensional
簡単 普通
Problem
81% Success 5582 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a string \(S\) that contains lowercase English letters. In one step, you can choose an index \(i\) from \(S\) and assign the next letter in the alphabet (\('a' \rightarrow 'b', 'b' \rightarrow 'c', \dots, 'z' \rightarrow 'a'\)) to \(S_i\). Define \(f(str)\) as the number of indices \(i \ (1 \le i < |str|)\) where \(str_i \neq str_{i + 1}\).

You are given a number \(K\). Now, your task is to determine the minimum number of steps required to perform on the string \(S\) to obtain a string \(S'\) such that \(f(S') \le K\).

Input format

  • First line: String \(S\) \((1 \le |S| \le 2048)\)
  • Second line: Integer \(K\) \((0 \le K < \min(|S|,32))\)

Output format

Print the minimum number of steps.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
CombinatoricsDynamic ProgrammingEasy
Points:30
63 votes
Tags:
Dynamic ProgrammingEasyOpenTwo dimensional
Points:30
22 votes
Tags:
AlgorithmsDynamic Programming2D dynamic programmingC++