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.

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.

题目链接:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description

题目大意:两个字符串中最长的字符串是否为另一字符串的子字符串,如果是,则返回-1,否则返回最长字符串长度。

思路:比较两个字符串的长度,若不相等,则返回长度的较大值,若相等则再判断两个字符串是否相同,若相同则返回-1,否则返回长度。

参考代码:

class Solution {
public:
    int findLUSlength(string a, string b) {
        int n = a.size() , m = b.size() ;
        if ( n != m )
            return max ( n , m ) ;
        if ( a == b )
            return -1 ;
        return n ;
    }
};

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 最长公共子序列(Longest Common Subsequence)指的是在两个序列中找到最长的公共子序列,这个公共子序列可以不连续,但是需要保持相对顺序不变。例如,对于序列ABCD和ACDFG,它们的最长公共子序列是ACD。 ### 回答2: 最长公共子序列(Longest Common Subsequence,简称LCS)是指在给定多个序列中,找到最长的一个子序列,该子序列同时出现在这些序列中,并且其他元素的相对顺序保持一致。 举个例子,假设有两个序列A和B,A为[1, 2, 3, 4, 5],B为[2, 4, 5, 6]。它们的一个最长公共子序列是[2, 4, 5],该子序列同时存在于A和B中。 求解LCS的问题可以用动态规划的方法来解决。我们可以构建一个二维数组dp,其中dp[i][j]表示序列A的前i个元素和序列B的前j个元素的LCS长度。那么dp[i][j]可以通过以下方式得到: 1. 如果A[i]等于B[j],则dp[i][j]等于dp[i-1][j-1] + 1; 2. 如果A[i]不等于B[j],则dp[i][j]等于max(dp[i-1][j], dp[i][j-1])。 通过填充整个dp数组,最终可以得到序列A和序列B的LCS长度。要找到具体的LCS序列,则可以通过反向遍历dp数组进行构建。 LCS问题在字符串处理、DNA序列匹配、版本控制等领域都有广泛的应用。其时间复杂度为O(m*n),其中m和n分别为序列A和序列B的长度。 ### 回答3: 最长公共子序列(Longest Common Subsequence)是一个经典的计算机科学问题。给定两个序列S和T,我们要找出它们之间最长的公共子序列。 子序列是从给定序列中按顺序选择几个元素而组成的序列。而公共子序列指的是同时是序列S和T的子序列的序列。 为了解决这个问题,可以使用动态规划的方法。我们可以定义一个二维数组dp,其中dp[i][j]表示序列S的前i个元素和序列T的前j个元素之间的最长公共子序列的长度。 接下来,我们可以使用以下递推关系来填充dp数组: 如果S[i]等于T[j],则dp[i][j] = dp[i-1][j-1] + 1; 如果S[i]不等于T[j],则dp[i][j] = max(dp[i-1][j], dp[i][j-1])。 最后,我们可以通过查看dp[S.length()][T.length()]来得到最长公共子序列的长度。 此外,我们也可以用回溯法来还原最长公共子序列本身。我们可以从dp[S.length()][T.length()]开始,如果S[i]等于T[j],则将S[i]添加到结果序列中,并向左上方移动,即i = i-1,j = j-1。如果S[i]不等于T[j],则根据dp数组的值选择向上(i = i-1)或向左(j = j-1)移动。 总之,最长公共子序列问题是一个经典的计算机科学问题,可以使用动态规划的方法解决。我们可以通过构建二维dp数组来计算最长公共子序列的长度,并可以使用回溯法来还原它本身。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值