当前位置:网站首页>Force deduction solution summary 522- longest special sequence II
Force deduction solution summary 522- longest special sequence II
2022-06-28 14:22:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Given string list strs , Return to it The longest special sequence . If the longest special sequence does not exist , return -1 .
Special sequence The definition is as follows : The sequence is a string Unique subsequence ( That is, it cannot be a subsequence of another string ).
s Of Subsequences can be made by deleting strings s Implementation of some characters in .
for example ,"abc" yes "aebdc" The subsequence , Because you can delete "aebdc" Use the underscore character in to get "abc" ."aebdc" The subsequence of also includes "aebdc"、 "aeb" and "" ( An empty string ).
Example 1:
Input : strs = ["aba","cdc","eae"]
Output : 3
Example 2:
Input : strs = ["aaa","aaa","aa"]
Output : -1
Tips :
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i] Only lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-uncommon-subsequence-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * First, find out all the duplicates in the string array , What is repeated must not be a unique subsequence . * Then sort the string array , Start long , Short traversal . * If the current string is repeated , Is added to the set . * If not, judge whether it is a subsequence of strings in the set , If not, it is the desired result . If it is , Then skip and continue to find the next .
Code :
public class Solution522 {
public int findLUSlength(String[] strs) {
Set<String> set = new HashSet<>();
Set<String> repeatSet = new HashSet<>();
for (String str : strs) {
if (set.contains(str)) {
repeatSet.add(str);
} else {
set.add(str);
}
}
Arrays.sort(strs, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.length() - o1.length();
}
});
Set<String> checkSet = new HashSet<>();
for (int i = 0; i < strs.length; i++) {
String str = strs[i];
if (repeatSet.contains(str)) {
checkSet.add(str);
continue;
}
// Judgment is not checkSet The subsequence in
if (isChild(checkSet, str)) {
continue;
}
return str.length();
}
return -1;
}
private boolean isChild(Set<String> checkSet, String str) {
if (checkSet.size() == 0) {
return false;
}
char[] strChars = str.toCharArray();
for (String checkStr : checkSet) {
// Judge str Is it right? checkStr The subsequence
char[] checkChars = checkStr.toCharArray();
int index = 0;
for (int i = 0; i < checkChars.length; i++) {
if (strChars[index] == checkChars[i]) {
index++;
}
if (index == str.length()) {
return true;
}
}
}
return false;
}
}边栏推荐
- Ruijie switch configuration SSH password login command [easy to understand]
- 2022年焊工(技师)考试题库模拟考试平台操作
- 排序
- 力扣解法汇总522-最长特殊序列 II
- 从小小线虫谈起——溯源神经系统进化,开启生命模拟
- sort
- 开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
- js 判断字符串为空或者不为空
- Based on asp Net based document retrieval system
- Native JS implements drag and drop of page elements
猜你喜欢

Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation

荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?

Opengauss kernel: analysis of SQL parsing process

物联网低代码平台常用《组件介绍》

How to design data visualization platform

Based on asp Net based document retrieval system

DevEco Studio 3.0编辑器配置技巧篇

你的代码会说话吗?(上)

A bug liver a week I can't help mentioning issue

一个bug肝一周...忍不住提了issue
随机推荐
猫狗队列
运行近20年,基于Win 98的火星探测器软件迎来首次升级
Leetcode(167)——两数之和 II - 输入有序数组
《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」
3. Overall UI architecture of the project
G : 最大流问题
RAM ROM FLASH的区别
Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
2022 welder (technician) examination question bank simulated examination platform operation
哪个证券公司最大最安全 怎么办理开户最安全
Robot range of motion (DFS)
Deveco studio 3.0 editor configuration tips
@Controlleradvice + @exceptionhandler handles controller layer exceptions globally
Thread life cycle and its methods
js 判断字符串为空或者不为空
一个bug肝一周...忍不住提了issue
你的代码会说话吗?(上)
Play NAS home NAS server setup scheme "suggestions collection"
如何设计数据可视化平台
接雨水系列问题