IT/Programming / / 2023. 4. 17. 10:28

[HackerRank] Day 6: Let's Review

반응형

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();
	}
}
 
www.hackerrank.com

 

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유