File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+
3+ 1. 아이디어 :
4+ 각 단어가 다른 단어의 접미사인지 확인
5+ 현재 단어가 다른 단어의 접미사라면 별도로 인코딩할 필요가 없음
6+ 두 단어의 뒤쪽 문자부터 직접 비교하여 접미사 여부를 판단
7+ 접미사가 아닌 단어만 단어 길이와 '#'의 길이 1을 더한다
8+
9+ 2. 시간복잡도 : O(N^2 × L) (N: 단어의 개수, L: 단어의 최대 길이)
10+ 자료구조/알고리즘 : 완전 탐색
11+
12+ */
13+
14+ class Solution {
15+ public int minimumLengthEncoding (String [] words ) {
16+ int answer = 0 ;
17+
18+ for (int i = 0 ; i < words .length ; i ++) {
19+ boolean isSuffix = false ;
20+
21+ for (int j = 0 ; j < words .length ; j ++) {
22+ if (i == j ) continue ;
23+
24+ int len1 = words [i ].length ();
25+ int len2 = words [j ].length ();
26+
27+ if (len1 > len2 ) continue ;
28+
29+ boolean same = true ;
30+
31+ for (int k = 1 ; k <= len1 ; k ++) {
32+ char c1 = words [i ].charAt (len1 - k );
33+ char c2 = words [j ].charAt (len2 - k );
34+
35+ if (c1 != c2 ) {
36+ same = false ;
37+ break ;
38+ }
39+ }
40+
41+ if (same ) {
42+ if (len1 == len2 && i < j ) {
43+ continue ;
44+ }
45+
46+ isSuffix = true ;
47+ break ;
48+ }
49+ }
50+
51+ if (!isSuffix ) answer += words [i ].length () + 1 ;
52+ }
53+
54+ return answer ;
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments