最长特殊序列 II

class Solution {
boolean containsSub(String s,String p){
int i,j;
for(i=0,j=0;i<p.length()&&j<s.length();j++)
if(s.charAt(j)==p.charAt(i))
i++;
if(i>=p.length())return true;
return false;
}
void removeStr(String p,LinkedList<String>list){
if(list.contains(p)){
String s="";
for(int i=0;i<list.size();i++){
s=list.get(i);
if(s.length()<p.length()&&containsSub(p,s)){
removeStr(s,list);
}
}
list.remove(p);
}
}
public int findLUSlength(String[] strs) {
int n=strs.length;
HashMap<String,Integer>map=new HashMap<>();
for(String s:strs)
if(map.containsKey(s))
map.replace(s, map.get(s)+1);
else
map.put(s, 1);
LinkedList<String>list=new LinkedList<>();
for(String s:map.keySet())
list.addFirst(s);
list.sort(new Comparator<String>(){ @Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return o2.length()-o1.length();
} });
int last=list.size(),i=0;
while(i<list.size()){
if(map.get(list.get(i))>1)
removeStr(list.get(i),list);
else
i++;
if(last==list.size())break;
else last=list.size();
}
if(list.size()==0)return -1;
return list.getFirst().length();
}
}

坑:判断包含关系和递归删除

最长特殊序列 II的更多相关文章

  1. Leetcode 522.最长特殊序列II

    最长特殊序列II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符实现,但 ...

  2. Java实现 LeetCode 522 最长特殊序列 II(查找最长的非子序列的长度)

    522. 最长特殊序列 II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些 ...

  3. [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  4. 522 Longest Uncommon Subsequence II 最长特殊序列 II

    详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...

  5. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  6. 九度oj题目1342:寻找最长合法括号序列II

    题目1342:寻找最长合法括号序列II(25分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:886 解决:361 题目描述: 假如给你一个由’(‘和’)’组成的一个随机的括号序列,当然 ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  9. P1091 合唱队形 DP 最长升序列维护

    题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...

随机推荐

  1. oracle和mysql的一些区别

    1.分页查询语句的区别 2.字符串拼接的区别,oracle不支持三个字符串的拼接,mysql支持三个字符串的拼接,在mybatis中,连接的是oracle数据库,字符串的拼接需要如下语句: <s ...

  2. 英语语法 - the + 形容词 的意义

    1,表示一类人  (复数) the young 青年 the old 老年the poor 穷人 the rich 富人the sick 病人 The old need care more than ...

  3. JDBC面试知识点整理(温习用)

    要面试,所以把之前的笔记整理一遍,嘻嘻,加油 JDBC编程 使用JDBC,java程序可以轻松地操作各种主流数据库,Oracle,MySQL,等,使用JDBC编写的程序不仅可以实现跨数据库,还具有跨平 ...

  4. Python MySQL 插入表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. 仿淘宝 vue

    最近自己闲着无聊,用vue仿照淘宝打算写个皮囊,顺便把遇到的问题顺便记录下 1.动画问题 (1)单个元素给动画 <transition name="fade">< ...

  6. linux系统pid的最大值研究

    内核源码探查 通过对linux内核源码的追踪,可以看到对pid最大值的限定最终集中到include/linux/threads.h文件中的PID_MAX_DEFAULT上了,代码如下: /* * Th ...

  7. POJ 2593&&2479:Max Sequence

    Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16329   Accepted: 6848 Des ...

  8. mybatis初步配置容易出现的问题

    The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...

  9. c++ STD Gems07

    reverse.rotate.permutation #include <iostream> #include <vector> #include <string> ...

  10. EL&JSTL简单介绍

    EL表达式 是为了简化咱们的jsp代码,具体一点就是为了简化在jsp里面写的那些java代码. 写法格式 ${表达式 } 如果从作用域中取值,会先从小的作用域开始取,如果没有,就往下一个作用域取. 一 ...