Problem · String

Rolling String

Learn this problem
MediumIBM logoIBMINTERNOA
See IBM hiring insights

Problem statement

Given a lowercase English string s and a list of operations, apply the operations in order and return the final string.

Each operation has the form "i j D", where i and j are 0-based inclusive indices and D is one of:

  • R: roll every character in s[i..j] one position forward in the alphabet, with z becoming a.
  • L: roll every character in s[i..j] one position backward in the alphabet, with a becoming z.

Function

rollingString(s: String, operations: String[]) → String

Examples

Example 1

s = "abc"operations = ["0 0 L", "2 2 L", "0 2 R"]return = "acc"
  1. 0 0 L changes abc to zbc.
  2. 2 2 L changes it to zbb.
  3. 0 2 R changes it to acc.

Therefore the final string is acc.

Constraints

  • 1 ≤ s.length ≤ 150
  • 1 ≤ operations.length ≤ 100
  • s contains only lowercase English letters.
  • Every operation has valid indices 0 ≤ i ≤ j < s.length and direction L or R.

More IBM problems

drafts saved locally
public String rollingString(String s, String[] operations) {
    // Write your code here
}
s"abc"
operations["0 0 L", "2 2 L", "0 2 R"]
expected"acc"
checking account