LeetCode --- string series --- longest special sequence I

Longest special sequence I

topic

Given two strings, you need to find the longest special sequence from the two strings.

The longest special sequence is defined as follows: the sequence is the longest subsequence unique to a certain string (that is, it cannot be a subsequence of other strings).

Subsequences can be achieved by deleting certain characters in the string, but the relative order of the remaining characters cannot be changed.

An empty sequence is a subsequence of all strings, and any string is its own subsequence.

The input is two strings, and the length of the longest special sequence is output. If it does not exist, it returns -1.


Examples

示例 1:

输入: "aba", "cdc"
输出: 3
解析: 最长特殊序列可为 "aba" (或 "cdc")

Source: LeetCode (LeetCode)

Link: https://leetcode-cn.com/problems/longest-uncommon-subsequence-i/ The

copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.


Problem-solving ideas

1、这个题目出的也是很迷......
实际上说的是两个字符串中,找出一个最长的独有的字符串序列。
这个序列只能是其中一个字符串的子序列,不能是另一个字符串的子序列

2、根据上面的规定,可以归类:
    若两个字符串 `长度相等` ,但是 `内容不等` ,那么该最长特殊序列为其中 `任意一个` 即可
    若两个字符串 `长度不等` ,那么 `长度最长的字符串` 一定不是 `短的字符串` 的子序列,所以最长的字符串即为最长子序列

answer

let findLUSlength = function(a, b) {
    // 两字符串相等,肯定不存在最长特殊序列
    if (a === b) return -1
    // 不相等,取最长的一个
    return Math.max(a.length, b.length)
}

Guess you like

Origin www.cnblogs.com/linjunfu/p/12680243.html