Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.
An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
"abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).Example 1:
Input: strs = ["aba","cdc","eae"]
Output: 3
Example 2:
Input: strs = ["aaa","aaa","aa"]
Output: -1
Constraints:
2 <= strs.length <= 501 <= strs[i].length <= 10strs[i] consists of lowercase English letters.impl Solution {
pub fn find_lu_slength(strs: Vec<String>) -> i32 {
let mut max_length = -1;
for i in 0..strs.len() {
// flag ~_~
let mut is_uncommon = true;
for j in 0..strs.len() {
if i != j && Solution::is_subsequence(&strs[i], &strs[j]) {
is_uncommon = false;
break;
}
}
if is_uncommon {
max_length = std::cmp::max(max_length, strs[i].len() as i32);
}
}
max_length
}
pub fn is_subsequence(s: &str, t: &str) -> bool {
let mut i = 0;
let mut j = 0;
// 문자열을 벡터로 변환
// why?! 러스트는 문자열을 UTF-8 로 인코딩되어 각 문자의 크기가 가변적이며,
// 이를 직접 접근하도록 지원하는 언어도 있지만... 러스트 이놈은 직접 접근을 지원 안함
let s_chars: Vec<char> = s.chars().collect();
let t_chars: Vec<char> = t.chars().collect();
while i < s_chars.len() && j < t_chars.len() {
if s_chars[i] == t_chars[j] {
i += 1;
}
j += 1;
}
i == s_chars.len()
}
}