본문 바로가기

Algorithms/30 Days of Code

Day 6: Let's Review

Task 
Given a string, , of length  that is indexed from  to , print its even-indexed and odd-indexed characters as  space-separated strings on a single line (see the Sample below for more detail).

Note:  is considered to be an even index.

Input Format

The first line contains an integer,  (the number of test cases). 
Each line  of the  subsequent lines contain a String, .

Constraints

Output Format

For each String  (where ), print 's even-indexed characters, followed by a space, followed by 's odd-indexed characters.

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

Explanation

Test Case 0 
 
 
 
 
 
 
The even indices are , and , and the odd indices are , and . We then print a single line of  space-separated strings; the first string contains the ordered characters from 's even indices (), and the second string contains the ordered characters from 's odd indices ().

Test Case 1 
 
 
 
 
The even indices are  and , and the odd indices are  and . We then print a single line of  space-separated strings; the first string contains the ordered characters from 's even indices (), and the second string contains the ordered characters from 's odd indices ().


최초풀이

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;


public class Solution {


    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 input = new Scanner(System.in);

        List<String> strings = new ArrayList<>();

        int testCase = input.nextInt();

        input.nextLine();

        for (int i = 0; i < testCase; i++) {

            strings.add(input.nextLine());

        }

        System.out.println(divideOddAndEvenString(strings));

    }


      private static String divideOddAndEvenString(List<String> strings) {

        StringBuilder result = new StringBuilder();

        int size = strings.size();

        if (size > 1) {

            for (int i = 0; i < size; i++){

                StringBuilder a = new StringBuilder();

                StringBuilder b = new StringBuilder();

                char[] chars = strings.get(i).toCharArray();

                for (int j = 0; j < chars.length; j++) {

                    if (j % 2 == 0) a.append(chars[j]);

                    else b.append(chars[j]);

                }

                result.append(a).append(" ").append(b).append("\n");

            }

            return result.toString();

        } else {

            StringBuilder a = new StringBuilder();

            StringBuilder b = new StringBuilder();

            char[] chars = strings.get(0).toCharArray();


            for (int i = 0; i < chars.length; i++) {

                if (i % 2 == 0) b.append(chars[i]);

                else a.append(chars[i]);

            }

            result.append(a).append(" ").append(b);

            return result.toString();

        }

    }

}


좀 더 최적화한 풀이

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;


public class Solution {


    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 input = new Scanner(System.in);

        int testCase = input.nextInt();

        for (int i = 0; i < testCase; i++) {

            String word = input.next();

            System.out.println(divideOddAndEvenString(word));

        }

    }


      private static String divideOddAndEvenString(String word) {

        String oddStr = "";

        String evenStr = "";

        for (int i = 0; i < word.length(); i++){

            if (i %2 == 0) oddStr += word.charAt(i);

            else evenStr += word.charAt(i);

        }

        return oddStr + " " + evenStr;

    }

}


'Algorithms > 30 Days of Code' 카테고리의 다른 글

Day 8: Dictionaries and Maps  (0) 2018.02.19
Day 7: Arrays  (0) 2018.02.19
Day 5: Loops  (0) 2018.02.13
Day 4: Class vs. Instance  (0) 2018.02.13
Day 3: Intro to Conditional Statements  (0) 2018.02.12