Leetcode Longest Uncommon Subsequence I
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description
题目:
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Example 1:
Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"),
because "aba" is a subsequence of "aba",
but not a subsequence of any other strings in the group of two strings.
Note:
- Both strings' lengths will not exceed 100.
- Only letters from a ~ z will appear in input strings.
题解:
两种情况,如果a == b, 那么就没有 uncommon subsequence, return -1.
a != b 那么就是 a, b中较长string为substring. 因为较长的string 不可能是较短string的substring.
Time Complexity: O(Math.min(a.length(), b.length())). 比较a, b是否相等用较短string长度时间.
Space: O(1).
AC Java:
public class Solution {
public int findLUSlength(String a, String b) {
if(a.equals(b)){
return -1;
}
return Math.max(a.length(), b.length());
}
}
跟上Longest Uncommon Subsequence II.
Leetcode Longest Uncommon Subsequence I的更多相关文章
- [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- LeetCode Longest Uncommon Subsequence II
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- Leetcode#521. Longest Uncommon Subsequence I(最长特殊序列 Ⅰ)
题目描述 给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符实现,但 ...
- LeetCode 521 Longest Uncommon Subsequence I 解题报告
题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...
- LeetCode: 521 Longest Uncommon Subsequence I(easy)
题目: anysubsequence of the other strings. A subsequence is a sequence that can be derived from one se ...
- 【leetcode】521. Longest Uncommon Subsequence I
problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
随机推荐
- freeswitch中集成使用ekho实现TTS功能一
Linux下安装freeswitch并集成ekho实现TTS 1. linux下安装freeswitch就不多介绍了,具体链接网址: http://www.8000hz.com/archives/14 ...
- 超酷Loading进度条
在线演示 本地下载
- 递归实现N皇后问题
其实是看到一位名为“活在二次元的伪触”的博主昨天还是前天写了篇这个题材的笔记,觉得有点意思,于是想自己来写写. 其实我发现上述那位同学写N皇后问题写得还不错,文末也会给出这位同学用通过递归的方法实现N ...
- Tomcat8内置jdk8运行环境发布web项目
简单说明:之前部署项目都是没有改变之前的环境变量,最近由于公司的数据源换了,jdk由1.7改成了1.8,tomcat7也改为了1.8,现在需要部署采用新数据源的这个项目, 为了不改变之前的环境变量,使 ...
- mysql里的ibdata1文件
mysql大多数磁盘空间被 InnoDB 的共享表空间 ibdata1 使用.而你已经启用了 innodb_file_per_table,所以问题是: ibdata1存了什么? 当你启用了innodb ...
- php-fpm docker 容器 搭建
继续上一篇文章(centos 7 容器的搭建) 下面构建一个php-fpm镜像: dockerfile 文件如下: # # MAINTAINER # DOCKER-VERSION # # Docker ...
- 研究Mac OS中Migration Assistant工具
一.对Migration Assistant工具的理解: 1.最精辟的定义是:Move your content to a new Mac. 如果想将windows上的数据迁移到mac上?使用工具Wi ...
- C中的指针和字符串
程序 #include<stdio.h>int main(void){ char *mesg="Don't be a fool!"; char *copy; copy= ...
- host capability
目前通过ComputeCapabilitiesFilter 是可以做到cpu 指令集的filter,ComputeCapabilitiesFilter 是通过flavor的extra_spe来传递的. ...
- Word 2010 怎么在每一章中使用不同的页眉
1.要做到每一章的页眉不同首先要进行 分节 word2010中 页面布局 -> 分隔符 ->下一页 上述操作即可实现分节 2.实现分节后,在每一节开头的那一页,编辑页眉 ...