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 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
随机推荐
- iframe 跨域请求
iframe.contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. iframe.contentDocument Firefox 支持,> ie8 的ie支持.可取得 ...
- Boostrap常用组件英文名
dropdownlisttabsearchVertical TabSidebar with tabssidebarExpandable Panel ListFiltered Attendees Lis ...
- 【HackerRank】Halloween party
Change language : Alex is attending a Halloween party with his girlfriend Silvia. At the party, Silv ...
- 20145210姚思羽《网络对抗》Web基础
20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...
- Go make 和 new的区别
在Go语言中: make 被用来分配引用类型的内存: map, slice, channel new 被用来分配除了引用类型的所有其他类型的内存: int, string, array等 本文主要给大 ...
- Cisco学习笔记
目录 1. 路由 1.1 静态路由 1.2 动态路由 2. 访问控制列表 2.1 标准访问控制列表 2.2 扩展访问控制列表 2.3 命名访问控制列表 3. VLAN 3.1 基础知识 3.2 配置实 ...
- JMeter学习(七)聚合报告之 90% Line 正确理解
90% Line 参数正确的含义: 虽然,我的上面理解有一定的道理,显然它是错误的.那看看JMeter 官网是怎么说的? 90% Line - 90% of the samples took no m ...
- IntelliJ Idea 快捷键精选
Alt+Enter,导入包,自动修正Ctrl+F,查找文本Alt+Up/Down,上/下移一行:在方法间快速移动定位:与光标位置有关Alt+Shift+Up,多次up可选中一行Ctrl+X,剪切行Ct ...
- myeclipse 10 j安装了JDK1.7,java编译器无法选择到1.7的问题
java程序编写,在eclipse中会自动编译,编译的版本在preferrence-->java-->compiler选择具体版本,这时你写程序时自动编译用的jdk就是这个版本的jdk,这 ...
- 算法总结之 在单链表和双链表中删除倒数第k个节点
分别实现两个函数,一个可以删除单链表中倒数第k个节点,另一个可以删除双链表中倒数第k个节点 思路: 如果链表为空,或者k<1 参数无效 除此之外 让链表从头开始走到尾,每移动一步,就让k的值减1 ...