[leetcode-521-Longest Uncommon Subsequence I]
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.
思路:
Simple analysis of this problem can lead to an easy solution.
These three cases are possible with string a and b:
a=b. If both the strings are identical, it is obvious that no subsequence will be uncommon. Hence, return -1.
length(a)=length(b) and a≠b. Example: abc and abd . In this case we can consider any string i.e. abc or abd as a required subsequence,as out of these two strings one string will never be a subsequence of other string. Hence, return length(a) or length(b).
length(a) ≠ length(b). Example abcd and abc. In this case we can consider bigger string as a required subsequence because bigger string can't be a subsequence of smaller string. Hence, return max(length(a),length(b)).
int findLUSlength(string a, string b)
{
if (a == b) return -;
if (a.length() == b.length()) return a.length();
return max(a.length(), b.length());
}
参考:
https://leetcode.com/articles/longest-uncommon-subsequence-i/
[leetcode-521-Longest Uncommon Subsequence I]的更多相关文章
- 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
problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
- 521. Longest Uncommon Subsequence I - LeetCode
Question 521. Longest Uncommon Subsequence I Solution 题目大意:给两个字符串,找出非共同子串的最大长度 思路:字符串相等就返回-1,不等就返回长度 ...
- 521. Longest Uncommon Subsequence I【easy】
521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode: 521 Longest Uncommon Subsequence I(easy)
题目: anysubsequence of the other strings. A subsequence is a sequence that can be derived from one se ...
- *521. Longest Uncommon Subsequence I (bit manipulation 2^n)
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- 521 Longest Uncommon Subsequence I 最长特殊序列 Ⅰ
给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列).子序列可以通过删去字符串中的某些字符实现,但不能改变剩余 ...
- 521. Longest Uncommon Subsequence I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- 转:Redis使用认证密码登录
Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...
- 转:简单的Mysql主从复制设置
步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修改主服务器master: #vim /etc/my.cn ...
- 每天一道Java题[2]
问题 可以直接根据hashCode()方法产生的值判断两个对象是否相等吗? 解答 不能!根据Wikipedia(https://en.wikipedia.org/wiki/Java_hashCode( ...
- JAVA THREAD.JOIN方法详解
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- 关于web程序快速开发个人见解以及经历
由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经历过两个系统的开发,也因为其他项目团队需 ...
- How to parse project properties or how to parse files with key-value pair
If a file has content like raven.enabled = false raven.host = "localhost" raven.port = 808 ...
- Ultimus BPM 零售和快消品行业应用解决方案
Ultimus BPM 零售和快消品行业应用解决方案 行业应用需求 中国零售及快消品行业正在经历深刻变化.经济下滑,消费回落,行业危机继续发酵:人员工资.房租费用进一步上涨,成本高涨成为不能承受之重: ...
- Spring Security教程系列(一)基础篇-1
第 1 章 一个简单的HelloWorld 第 1 章 一个简单的HelloWorld Spring Security中可以使用Acegi-1.x时代的普通配置方式,也可以使用从2.0时代才出现的命名 ...
- 【Netty】ChannelHandler和ChannelPipeline
一.前言 前面学习了Netty的ByteBuf,接着学习ChannelHandler和ChannelPipeline. 二.ChannelHandler和ChannelPipeline 2.1 Cha ...
- 关于EF中直接执行sql语句的参数化问题
某天 , 在review项目中代码的时候, 发现有哥们直接通过 Database.ExecuteSqlCommand("select * from order_info where com ...