We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接: https://leetcode-cn.com/problems/longest-uncommon-subsequence-ii
难度: Medium 标签: 数组 哈希表 双指针 字符串 排序
Medium
数组
哈希表
双指针
字符串
排序
The text was updated successfully, but these errors were encountered:
Difficulty: 中等
Related Topics: 数组, 哈希表, 双指针, 字符串, 排序
给定字符串列表 strs ,返回其中 最长的特殊序列 。如果最长特殊序列不存在,返回 -1 。
strs
-1
特殊序列 定义如下:该序列为某字符串 独有的子序列(即不能是其他字符串的子序列)。
s 的 子序列可以通过删去字符串 s 中的某些字符实现。
s
"abc"
"aebdc"
"a<u>e</u>b<u>d</u>c"
"aeb"
示例 1:
输入: strs = ["aba","cdc","eae"] 输出: 3
示例 2:
输入: strs = ["aaa","aaa","aa"] 输出: -1
提示:
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i]
思路:
Language: JavaScript
/** * @param {string[]} strs * @return {number} */ var findLUSlength = function(strs) { strs.sort((a, b) => b.length - a.length); //按长度递减排序 const n = strs.length; for (let i = 0; i < n; i++) { let isUncommon = true; for (let j = 0; j < n; j++) { if (i === j) continue; if (isSubSequece(strs[i], strs[j])) { // 检查当前字符串是否是其他字符串的子序列 isUncommon = false; break; } } if (isUncommon) return strs[i].length; // 因为已经长度递减排序,因此返回当前字符串的长度 } return -1; }; const isSubSequece = (str1, str2) => [...str2].reduce((pos, str) => str1[pos] === str ? pos + 1 : pos, 0) === str1.length;
Sorry, something went wrong.
No branches or pull requests
题目链接: https://leetcode-cn.com/problems/longest-uncommon-subsequence-ii
难度:
Medium
标签:
数组
哈希表
双指针
字符串
排序
The text was updated successfully, but these errors were encountered: