521. Longest Uncommon Subsequence I【easy】

news/2024/7/4 11:03:58

521. Longest Uncommon Subsequence I【easy】

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:

  1. Both strings' lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.

 

解法一:

 1 class Solution {
 2 public:
 3     int findLUSlength(string a, string b) {
 4         if (a.size() != b.size()) {
 5             return max(a.size(), b.size());
 6         }
 7         else {
 8             return (a == b ? -1 : a.size());
 9         }
10     }
11 };

 

转载于:https://www.cnblogs.com/abc-begin/p/7581878.html


http://www.niftyadmin.cn/n/3371132.html

相关文章

数据库的一些基础知识

数据库(Data Base) 数据库(Data Base,简称DB)是长期存储在计算机内、有组织的、可共享的、统一管理的相关数据的集合。 关系型数据库 绝大多数的数据库系统叫做关系数据库系统(relational database system&…

穷忙?~

一直以来,都觉得自己的生活过得好忙碌。。。。读书的时候,常常忙着去上课,或者去吃饭,因为吃完饭,还要忙着去宿舍休息(中学的时候是要按时休息的),或者重新赶去上课。初中的时候,有个…

【转】将一棵树转换为二叉树后,为什么根节点没有右子树

树转化为二叉树时结点左子树是原来的孩子结点,右子树是原来的兄弟结点。即取根节点左孩子向右连接他的兄弟结点(在同一层次的节点,原来互不相连)并把它的子树,而把除左孩子外,原来与根节点相连的线擦除。这…

python之路_面向对象进阶

一、内置函数isinstance和issubclass 1、isinstance() isinstance(obj,cls)检查obj是否是类 cls 的对象,类似type()。 class Foo(object):pass obj Foo() print(isinstance(obj, Foo)) #输出结果:Trueprint(isinstance(10,int)) #输出结果&…

21、Samba配置详解

1、SMB介绍Samba可以实现Linux和Windows的文件共享 SMB(Service Message Block)涉及的端口smb:tcp 139 445nmbd udp 137 138samba用户是系统用户但密码是独立的,非/etc/shadow中的密码,使用smbpasswd设置访问smb的…

香甜的黄油 Sweet Butter

原题链接:https://www.luogu.org/problem/show?pid1828#sub 经典的最短路问题。 各位不要被题目条件迷惑了,牧场想象成点,道路想象成边,奶牛所在的位置想象成点权就好。 输入的是无向图,所以在正向连边时反向连边。然…

SSH简介及配置使用SSH登陆远程主机

作者Blog:http://blog.csdn.net/s98/原创:房东雨 2004年9月6日1.什么是SSH传统的网络服务程序,如:ftp、POP和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就…

15. 进制转换

手工转进制 // 7FF 16转10进制 console.log(15 15*16 7*16**2) 未完... 待续转载于:https://www.cnblogs.com/zouxinping/p/10633616.html