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 ab. 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]的更多相关文章

  1. Leetcode#521. Longest Uncommon Subsequence I(最长特殊序列 Ⅰ)

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

  2. LeetCode 521 Longest Uncommon Subsequence I 解题报告

    题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...

  3. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

  4. 521. Longest Uncommon Subsequence I - LeetCode

    Question 521. Longest Uncommon Subsequence I Solution 题目大意:给两个字符串,找出非共同子串的最大长度 思路:字符串相等就返回-1,不等就返回长度 ...

  5. 521. Longest Uncommon Subsequence I【easy】

    521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...

  6. 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode: 521 Longest Uncommon Subsequence I(easy)

    题目: anysubsequence of the other strings. A subsequence is a sequence that can be derived from one se ...

  8. *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 ...

  9. 521 Longest Uncommon Subsequence I 最长特殊序列 Ⅰ

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

  10. 521. Longest Uncommon Subsequence I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. OpenStack修复影响宿主机的QEMU漏洞CVE-2017-2615

    距离这个虚拟化层面的漏洞公告发出已有两个多月了,漏洞详情可以查看: 360安全应急响应中心-360发现QEMU严重漏洞 影响国内大部分公有云 简单来说是通过Cirrus VGA操作读取宿主机内存中的内 ...

  2. 常用的CI笔记

    1. thinkphp 封装好的$this->success(),就直接实现成功跳转,$this->error(),错误跳转.CI有show_error(),但是却不能直接实现跳转,所以需 ...

  3. 高效工作的秘诀——Doit.im使用总结报告

    从上次购买doit.im pro账户到现在已经快一年了,从摸索到现在的熟悉,目前这款软件已经成为我工作生活中最为重要的效率工具,在此之前也用过很多软件进行时间管理,综合起来评价,doit应该算是最棒的 ...

  4. xml 和html 语言区别

    都是标记语言(ML),一个是超文本标记语言,一个是扩展标记语言. 不同之处: 1可扩展性:HTML不具备扩展性,而XML是原标记语言,可以用于定义新的标记语言. 2侧重点: HTML侧重于如何表现信息 ...

  5. Java生成二维码--QRGen

    最近公司需求需要生成一个二维码 , 由于之前没有接触过 , 故此做个记录 . 在网上找到了不少二维码生成工具,都蛮好用的. 不过要集成二维码生成功能到应用开发中,就要选择最好用成熟的库了,最终决定采用 ...

  6. Lua学习(5)——迭代器与泛型for

    1. 迭代器 2. 泛型for语义 所谓迭代器就是一种可以遍历一种集合中所有元素的机制.在lua中,迭代器通常表示为函数,每调用依次函数就返回集合中的下一个元素.泛型for 内部保存了迭代器函数 实际 ...

  7. 源码阅读—Iterator接口和LIstIterator接口

    在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的. 我们知道,接口只是一种规范,当继承接口并实现其中的方 ...

  8. Kotlin入门第三课:数据类型

    前文链接: Kotlin学习第一课:从对比Java开始 Kotlin入门第二课:集合操作 初次尝试用Kotlin实现Android项目 Kotlin的数据类型与Java类似,因此这篇文章主要看Kotl ...

  9. Spring component-scan 的逻辑 、单例模式下多实例问题、事务失效

    原创内容,转发请保留:http://www.cnblogs.com/iceJava/p/6930118.html,谢谢 之前遇到该问题,今天查看了下 spring 4.x 的代码 一,先理解下 con ...

  10. VR全景智慧城市,开启“上帝视角”体验‘身临其境’

    VR全景把不同的场景 分为若干个VR视角点 进入一个视角点,用户便能开启"上帝视角" 转动手机,身临其境地360度转动察看,对场景的全貌和细节一目了然.   人生处处有尴尬 比如大 ...