Skip to content

Commit 41ae9bf

Browse files
committed
522. Longest Uncommon Subsequence II
1 parent 5ac1564 commit 41ae9bf

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
1. 아이디어 : 모든 주어진 문자열중 중복된 부분문자열이 아닌 문자열중 가장 긴 문자열의 길이와 개수를 구한다.
4+
5+
2. 시간복잡도 : O(N^2*N)
6+
7+
3. 자료구조/알고리즘 : 투포인터, 완전탐색
8+
9+
*/
10+
11+
class Solution {
12+
public int findLUSlength(String[] strs) {
13+
// 안겹치는 가장 긴 문자열의 길이 (모든 문자열 중)
14+
15+
int ans = -1;
16+
17+
// aba cdc eae
18+
// aa aaa aaa
19+
20+
for(int i=0; i<strs.length; i++) {
21+
boolean check = true;
22+
for(int j=0; j<strs.length; j++) {
23+
if(i == j) continue;
24+
if(strs[i].length() > strs[j].length()) continue;
25+
26+
if(isSubsequence(strs[i], strs[j])) check = false;
27+
}
28+
29+
if(check) ans = Math.max(ans, strs[i].length());
30+
}
31+
32+
return ans;
33+
}
34+
35+
private boolean isSubsequence(String s1, String s2) {
36+
int idx1 = 0;
37+
int idx2 = 0;
38+
39+
while(idx1<s1.length() && idx2<s2.length()) {
40+
if(s1.charAt(idx1) == s2.charAt(idx2)) idx1++;
41+
idx2++;
42+
}
43+
44+
return idx1 == s1.length();
45+
}
46+
}

0 commit comments

Comments
 (0)