Header Ad

Leetcode Longest Uncommon Subsequence I problem solution

In this Leetcode Longest Uncommon Subsequence I problem solution we have Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between two strings is a string that is a subsequence of one but not the other.

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

For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).

leetcode Longest Uncommon Subsequence I problem solution

Problem solution in Python.

class Solution:
    def findLUSlength(self, A: str, B: str) -> int:
        if A == B:
            return -1
        return max(len(A), len(B))

Problem solution in Java.

class Solution {
    public int findLUSlength(String a, String b) {
            return -1;
        return Math.max(a.length(),b.length());

Problem solution in C++.

class Solution {
     int findLUSlength(string a, string b) {
        if(a == b) return -1;
        int x = a.length();
        int y = b.length();
        return (x>y ) ? x : y ; 

Problem solution in C.

int findLUSlength(char* a, char* b) {
    if(!a && !b)
        return 0;
    if(strcmp(a, b) != 0)
        if(strlen(a) >= strlen(b))
            return strlen(a);
            return strlen(b);
    return -1;

Post a Comment