Shift the letters
Practice
3.7 (30 votes)
Algorithms
Dynamic programming
Easy
Two dimensional
簡単 普通
Problem
81% Success 5583 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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
11 votes
Tags:
2-D ArrayAlgorithmsDynamic ProgrammingEasy
Points:30
6 votes
Tags:
ImplementationAlgorithmsBasics of bit manipulationDynamic Programming2D dynamic programming
Points:30
5 votes
Tags:
CombinatoricsDynamic ProgrammingEasy
Editorial
Login to unlock the editorial