classSolution: deffindLUSlength(self, strs: List[str]) -> int: defisSubsequence(s: str, t: str) -> bool: i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: i += 1 j += 1 return i == len(s) ans = -1 for i, s inenumerate(strs): for j, t inenumerate(strs): if i != j and isSubsequence(s, t): break else: ans = max(ans, len(s)) return ans
classSolution { public: intfindLUSlength(vector<string>& strs){ int n = strs.size(), ans = -1; for (int i = 0; i < n; i++) { bool flag = true; for (int j = 0; j < n; j++) { if (i != j && isSubsequence(strs[i], strs[j])) { flag = false; break; } } if (flag) ans = max(ans, (int)strs[i].size()); } return ans; }
boolisSubsequence(string s, string t){ int m = s.size(), n = t.size(); int i = 0, j = 0; while (i < m && j < n) { if (s[i] == t[j++]) i++; } return i == m; } };
classSolution: deffindLUSlength(self, strs: List[str]) -> int: defisSubsequence(s: str, t: str) -> bool: i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: i += 1 j += 1 return i == len(s) strs.sort(key=lambda x: len(x), reverse=True) for i, s inenumerate(strs): for j, t inenumerate(strs): if i != j and isSubsequence(s, t): break else: returnlen(s) return -1
classSolution { public: intfindLUSlength(vector<string>& strs){ int n = strs.size(); sort(strs.begin(), strs.end(), [](const string& a, const string& b) { return a.size() > b.size(); }); for (int i = 0; i < n; i++) { bool flag = true; for (int j = 0; j < n; j++) { if (i != j && isSubsequence(strs[i], strs[j])) { flag = false; break; } } if (flag) return strs[i].size(); } return-1; }
boolisSubsequence(string s, string t){ int m = s.size(), n = t.size(); int i = 0, j = 0; while (i < m && j < n) { if (s[i] == t[j++]) i++; } return i == m; } };
classSolution: deffindLUSlength(self, strs: List[str]) -> int: defisSubsequence(s: str, t: str) -> bool: i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: i += 1 j += 1 return i == len(s) cnt = Counter(strs) strs = list(cnt.keys()) uniques = [s for s, v in cnt.items() if v == 1] uniques.sort(key=lambda x: len(x), reverse=True) for s in uniques: for t in strs: if s != t and isSubsequence(s, t): break else: returnlen(s) return -1
classSolution { public: intfindLUSlength(vector<string>& strs){ int n = strs.size(); unordered_map<string, int> cnt; for (string& s : strs) cnt[s]++; vector<string> uniques; for (auto kv : cnt) if (kv.second == 1) uniques.push_back(kv.first); sort(uniques.begin(), uniques.end(), [](const string& a, const string& b) { return a.size() > b.size(); }); for (string& s : uniques) { bool flag = true; for (string& t : strs) { if (s != t && isSubsequence(s, t)) { flag = false; break; } } if (flag) return s.size(); } return-1; }
boolisSubsequence(string s, string t){ int m = s.size(), n = t.size(); int i = 0, j = 0; while (i < m && j < n) { if (s[i] == t[j++]) i++; } return i == m; } };
寫在最後
Cover photo is generated by @ゴリラの素材屋さん, thanks for their work!