String-o-Permute – HackerRank Solution

In this post, we will solve String-o-Permute HackerRank Solution. This problem (String-o-Permute) is a part of HackerRank Functional Programming series.


Kazama gave Shaun a string of even length, and asked him to swap the characters at the even positions with the next character. Indexing starts at 0.

Formally, given a string str of length L where L is even, Shaun has to swap the characters at position i and i + 1, where i = {0, 2, . . . , L – 2}.

For example, if str = “abcdpqrs”L = 8. We have to swap the characters at positions:
{(0, 1), (2, 3), (4, 5), (6, 7)}

So, answer will be “badcqpsr”.

Input Format

The first line contains an integer, T, the number of test cases.
T lines follow, each containing some string str.

Output Format

For each test case, print the new string as explained in the problem statement.


  • 1 <= T <= 10
  • 1 < L <= 105
  • L is even
  • str consists of lowercase English characters, {az}.

Sample Input


Sample Output



Test case #00: This is the same example as mentioned in the problem statement.
Test case #01: Here L is 2, so we have to swap the characters at position (0, 1) only.

Solution – String-o-Permute


import java.util.Scanner

object Solution {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(
    val t = sc.nextInt

    (0 until t).foreach(_ => {
      val s = sc.nextLine()
      println(s.grouped(2).map(i => i(1).toString + i(0)).mkString(""))

Note: This problem (String-o-Permute) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *