본문 바로가기

코딩테스트

CodeSignal / Intro / Smooth Sailing (1/2)

 

Ⅸ. All Longest Strings

 

Given an array of strings, return another array containing all of its longest strings.

// 가장 긴 문자열 출력

Example

ForinputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be

allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

Input/Output

[execution time limit] 3 seconds (java)

[input] array.string inputArray

A non-empty array.

Guaranteed constraints:

1 ≤ inputArray.length ≤ 10,

1 ≤ inputArray[i].length ≤ 10.

[output] array.string

Array of the longest strings, stored in the same order as in theinputArray.

 

》》

String[] allLongestStrings(String[] inputArray) {

    String x; // 각 문자열 담을 변수 선언

    int max=0; // 가장 긴 문자열의 길이를 저장할 변수 선언

    ArrayList<String> arr= new ArrayList<>(); // 가장 긴 문자열 저장할 리스트

    for(int i=0;i<inputArray.length;i++){ // max 값을 구한다.

x=inputArray[i];

max=max<x.length()?x.length():max;

}

    for(int j=0;j<inputArray.length;j++){ // max 값을 가지는 문자열을 리스트에 저장

x=inputArray[j];

if( max==x.length() ){

arr.add(x);

}

}

    String[] answer=arr.toArray(new String[arr.size()]); // 리스트를 배열형태로 변환

return answer;

}

 

Ⅹ. commonCharacterCount

 

Given two strings, find the number of common characters between them.

Example

Fors1 = "aabcc"ands2 = "adcaa", the output should be

commonCharacterCount(s1, s2) = 3.

Strings have3common characters -2"a"s and1"c".

Input/Output

[execution time limit] 3 seconds (java)

[input] string s1

A string consisting of lowercase English letters.

Guaranteed constraints:

1 ≤ s1.length < 15.

[input] string s2

A string consisting of lowercase English letters.

Guaranteed constraints:

1 ≤ s2.length < 15.

[output] integer

 

》》

int commonCharacterCount(String s1, String s2) {

int count=0;

String[] arr_s1=s1.split(""); // 배열에 한 글자씩 저장

String[] arr_s2=s2.split("");

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

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

if( arr_s1[i].equals(arr_s2[j])){ // == 로 비교하면 if문 안걸림

arr_s2[j]=" "; // arr_s2[j] 값 삭제 (임의로 값만 변경하여 겹치지 않게 )

count++;

break; // j for문 빠져나감

}

}

}return count;

}


☆ .equals() 과 == 의 차이

 

equals() : 메소드 / 비교하는 대상(객체)의 내용을 비교

== : 연산자 / 비교하는 대상(객체)의 주소값 비교


ⅩⅠ. isLucky

 

Ticket numbers usually consist of an even number of digits. A ticket number is consideredluckyif the sum of the first half of the digits is equal to the sum of the second half.

Given a ticket numbern, determine if it'sluckyor not.

// 주어진 숫자 n을 반으로 나눠 첫번째 반의 합이 두번째 반의 합과 같을 경우 Lucky 티켓 넘버라고 한다.

Example

Forn = 1230, the output should be // 12 | 30 --> 1+2=3+0

isLucky(n) = true;

Forn = 239017, the output should be // 2+3+9 != 0+1+7 --> unLucky

isLucky(n) = false.

Input/Output

[execution time limit] 3 seconds (java)

[input] integer n

A ticket number represented as a positive integer with an even number of digits.

Guaranteed constraints:

10 ≤ n < 106.

[output] boolean

trueifnis a lucky ticket number,falseotherwise.

 

》》 숫자 -> 문자 -> 숫자로 변환해서 구했음 ( 숫자를 한 자리씩 나누기 위해 )

boolean isLucky(int n) {

String sn =Integer.toString(n); // 주어진 숫자를 문자열로 변환

String[] arr_sn = sn.split(""); // 배열에 한 단어씩 저장

int sum1=0;

int sum2=0;

boolean Lucky=true;

for(int i=0;i<sn.length()/2;i++){

// 반으로 나눈 첫번쨰 그룹의 합 & Integer.parseInt(arr_sn[i]) 배열에 저장한 한 글자를 숫자형으로 변환해서 덧셈

sum1=sum1+Integer.parseInt(arr_sn[i]);

}

for(int i=sn.length()-1;i>sn.length()/2-1;i--){

sum2=sum2+Integer.parseInt(arr_sn[i]); // 두번째 그룹의 합

}

if(sum1==sum2){ // 같으면 럭키티켓

return Lucky;

}else return false; // 아니면 false

}