[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์˜์–ด ๋๋ง์ž‡๊ธฐ (์ž๋ฐ” java)

 

1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/12981?language=java

 

2. ํ’€์ด

 

3. ์ฝ”๋“œ

class Solution {
    public int[] solution(int n, String[] words) {
        int[] answer = {0,0};
        int temp1 = 0;
                
        for(int i=1;i<words.length;i++){
            String word = words[i];
            String prev = words[i-1];
            int plen = prev.length()-1;
            
            if(word.charAt(0) != prev.charAt(plen)){
                temp1 = i+1;
                break;
            }
            
            boolean check = false;
            for(int j = 0;j<i;j++){
                if(word.equals(words[j])){
                    temp1 = i+1;
                    check = true;
                    break;
                }
            }
            if(check == true) break;
        }
        
        if(temp1 == 0) return answer;
        int first = temp1%n;
        int second = temp1/n;
        
        answer[0] = first == 0? n:first;
        answer[1] = first == 0? second:second+1;
        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ