# 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)}

## 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.

## Constraints

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

Sample Input

``````2
abcdpqrs
az``````

Sample Output

``````badcqpsr
za``````

Explanation

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

Scala

```import java.util.Scanner

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

(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.