Problem · String

Alternate String Ends

Learn this problem
EasyCapital OneOA

Problem statement

Given a string s, return a new string formed by reading characters from alternating ends.

Use the characters in this order:

  1. Take the first character.
  2. Take the last character.
  3. Take the second character.
  4. Take the second-to-last character.
  5. Continue moving inward until every character has been used exactly once.

Equivalently, for a string of length n, visit indices 0, n - 1, 1, n - 2, and so on.

Function

alternateStringEnds(s: String) → String

Examples

Example 1

s = "abcde"return = "aebdc"

The characters are taken from indices 0, 4, 1, 3, and 2, producing aebdc.

Example 2

s = "abcdef"return = "afbecd"

The characters are taken from indices 0, 5, 1, 4, 2, and 3, producing afbecd.

Example 3

s = "x"return = "x"

A one-character string has the same character at the only position, so the result is x.

More Capital One problems

drafts saved locally
public String alternateStringEnds(String s) {
  // Write your code here.
}
s"abcde"
expected"aebdc"
checking account