521. Longest Special Sequence I

Hits: 0

address:

topic:

Given two [strings]  a and b, return the length of the longest special sequence of the two strings. Returns -1 if not present.

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

A subsequence of string s is the string that can be obtained after removing any number of characters from s.

For example, “abc” is a subsequence of “aebdc” because “abc” is obtained by removing the italicized and bold characters in “aebdc”. Subsequences of “aebdc” also include “aebdc” , “aeb” , and “” (the empty string).

Example 1:

Input: a = “aba”, b = “cdc”
Output: 3
Explanation: The longest special sequence can be “aba” (or “cdc”), both of which are subsequences of itself and not the other.

Example 2:

Input: a = “aaa”, b = “bbb”
Output: 3
Explanation: The longest special sequence is “aaa” and “bbb”.

Example 3:

Input: a = “aaa”, b = “aaa”
Output: -1
Explanation: Every subsequence of string a is also every subsequence of string b. Likewise, every subsequence of string b is also a subsequence of string a.

hint:

1 <= a.length, b.length <= 100
a and b consist of lowercase English letters

Source: LeetCode
Link: https://leetcode-cn.com/problems/longest-uncommon-subsequence-i The
copyright belongs to LeetCode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Ideas:

I misunderstood this question again, for example:

a = “abc”,b = “aebdc”

Thinking that if the lengths are not equal, find out whether the characters of the short string appear in sequence in the long string.

As a result, give me a length of b, and I will have an epiphany. I don’t know if you have an epiphany. Look at the code.

int findLUSlength(char * a, char * b){
    int lenA = strlen(a);
    int lenB = strlen(b);
    int retlen = 0;

    if(lenA == lenB)
    {
        if(strcmp(a, b) == 0)
            return -1;
        else
            return lenA;
    }

    return lenA > lenB ? lenA : lenB;
}

View more brushing notes

You may also like...

Leave a Reply

Your email address will not be published.