File tree 3 files changed +69
-0
lines changed
0521.Longest Uncommon Subsequence I
0522.Longest Uncommon Subsequence II
3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean detectCapitalUse (String word ) {
3
+ char [] cs = word .toCharArray ();
4
+ int upper = 0 ;
5
+ int lower = 0 ;
6
+ for (int i = 0 ; i < cs .length ; i ++) {
7
+ if (cs [i ] >= 'a' ) {
8
+ lower ++;
9
+ } else {
10
+ upper ++;
11
+ }
12
+ }
13
+ if (upper == cs .length ) {
14
+ return true ;
15
+ }
16
+ if (lower == cs .length ) {
17
+ return true ;
18
+ }
19
+ if (upper == 1 && cs [0 ] < 'a' ) {
20
+ return true ;
21
+ }
22
+ return false ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findLUSlength (String a , String b ) {
3
+ if (a .equals (b ))
4
+ return -1 ;
5
+ return Math .max (a .length (), b .length ());
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findLUSlength (String [] strs ) {
3
+ int res = -1 ;
4
+ if (strs == null || strs .length == 0 ) {
5
+ return res ;
6
+ }
7
+ if (strs .length == 1 ) {
8
+ return strs [0 ].length ();
9
+ }
10
+ // 两两比较
11
+ // 1、存在子串,直接不比较后面的字符串
12
+ // 2、不存在子串,判断当前字符串是否是最长的字符串
13
+ for (int i = 0 , j ; i < strs .length ; i ++) {
14
+ for (j = 0 ; j < strs .length ; j ++) {
15
+ if (i == j ) {
16
+ continue ;
17
+ }
18
+ // 根据题意,子串 可以 不是 原字符串中 连续的子字符串
19
+ if (isSubsequence (strs [i ], strs [j ])) {
20
+ break ;
21
+ }
22
+ }
23
+ if (j == strs .length ) {
24
+ res = Math .max (res , strs [i ].length ());
25
+ }
26
+ }
27
+ return res ;
28
+ }
29
+
30
+ public boolean isSubsequence (String x , String y ) {
31
+ int j = 0 ;
32
+ for (int i = 0 ; i < y .length () && j < x .length (); i ++) {
33
+ if (x .charAt (j ) == y .charAt (i ))
34
+ j ++;
35
+ }
36
+ return j == x .length ();
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments