티스토리 뷰

문제

https://programmers.co.kr/learn/courses/30/lessons/12904?language=java


풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution
{
    public int solution(String s)
    {
        int len = s.length();
        int max = 1;
        int tMax = 1;
        int p = 0;
        for(int i=0; i<len-1; i++) {
            tMax = 1;
            if(s.charAt(i) == s.charAt(i+1)) {
                int t = i+i+1;
                tMax = 0;
                for(int j=i+1; j<=len-1; j++) {
                    try {
                        char left = s.charAt(t-j);
                        char right = s.charAt(j);
                        if(left == right) {
                            tMax+=2;
                        }else {
                            break;
                        }
                    }catch(Exception e) {
                        break;
                    }
                }
            }
            if(tMax > max)
                max = tMax;    
            tMax = 1;
                for(int j=i-1; j>=0; j--) {
                    try {
                        char left = s.charAt(j);
                        char right = s.charAt(i+(i-j));
                        if(left == right) {
                            tMax+=2;
                        }else {
                            break;
                        }
                    }catch(Exception e) {break;}
                }
                if(tMax > max)
                    max = tMax;    
            }
        return max;
    }
}
cs

생각

풀기시작하고나서 직감적으로 쉽게는 안되겠구나 생각이들었고, Case가 적으므로 브루트포스를 통해 풀었다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday