Longest Uncommon Subsequence I

原题: https://leetcode.com/problems/longest-uncommon-subsequence-i/description/

题意: 给定两个字符串,计算其“最长不公共子序列”。最长不公共子序列是指:两字符串中某一个的子序列,该子序列不是另一个字符串的子序列,并且长度最长。子序列是指从一个序列中删除一些字符,剩余字符顺序保持不变得到的新序列。任何字符串都是其本身的子序列,空串不属于任意字符串的子序列。返回最长不公共子序列,若不存在,返回-1。

约定:(1)两字符串长度均不超过100;(2)输入字符串只包含小写字母a-z。

例子: 

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. 

标签: subsequence、序列、aba、uncommon、longest、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • SLPH
    2017-08-12 14:59:02 1楼#1层
    Python:
    class Solution(object):
        def findLUSlength(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: int
            """
            return a != b and max(len(a), len(b)) or -1
  • SLPH
    2017-08-12 14:59:52 2楼#1层
    Java:
    public class Solution {  
        public int findLUSlength(String a, String b) {  
            int lenA = a.length();  
            int lenB = b.length();  
            if (lenA != lenB)  
                return Math.max(lenA, lenB);  
            else if (!a.equals(b))  
                return lenA;  
            else  
                return -1;  
        }  
    }
  • SLPH
    2017-08-12 15:00:39 3楼#1层
    C++:
    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 ;  
        }  
    };
  • 回复
隐藏