반응형
Objective
Today we're expanding our knowledge of Strings and combining it with what we've already learned about loops. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given a string, S, of length N that is indexed from 0 to N - 1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).
Note: 0 is considered to be an even index.
Input Format
The first line contains an integer, T (the number of test cases).
Each line of the T subsequent lines contain a String, S.
Constraints
- 1 <= T <=10
- 2 <= length of S <= 10000
Sample Input
2 Hacker Rank
Sample Output
Hce akr Rn ak
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
/*
* Enter your code here. Read input from STDIN. Print output to STDOUT. Your
* class should be named Solution.
*/
Scanner sc = new Scanner(System.in);
int userCount = sc.nextInt();
char[] userInput = new char[10000];
ArrayList<ArrayList<Character>> totalResult = new ArrayList<ArrayList<Character>>();
int enter = 0;
for(int i = 0 ; i < userCount ; i++) {
userInput = sc.next().toCharArray();
ArrayList<Character> evenResult = new ArrayList<Character>();
ArrayList<Character> oddResult = new ArrayList<Character>();
for(int j = 0 ; j < userInput.length ; j++) {
if(j%2 == 0) {
evenResult.add(userInput[j]);
}else {
oddResult.add(userInput[j]);
}
}
totalResult.add(evenResult);
totalResult.add(oddResult);
}
for(ArrayList<Character> tmp : totalResult) {
for(int j = 0 ; j < tmp.size() ; j++) {
System.out.print(tmp.get(j));
}
enter++;
if(enter%2 == 0) {
System.out.println();
continue;
}
System.out.print(" ");
}
sc.close();
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[HackerRank] Day 8: Dictionaries and Maps (0) | 2023.04.17 |
---|---|
[HackerRank] Day 7: Arrays (0) | 2023.04.17 |
[HackerRank] Day 5: Loops (0) | 2023.04.17 |
[HackerRank] Day 4: Class vs. Instance (0) | 2023.04.17 |
[HackerRank] Day 3: Intro to Conditional Statements (0) | 2023.04.17 |