Longest Uncommon Subsequence I

Given two strings a and b, find the length of the longest uncommon subsequence between them.
An uncommon subsequence between two strings is a string that is a subsequence of one but not the other.

Find the length of the longest uncommon subsequence between a and b. If the longest uncommon subsequence doesn't exist, return -1.

Example 1:

Input: a = "aba", b = "cdc"
Output: 3

Approach

Java

public class LongestUncommonSubsequenceI {
    public static void main(String[] args) {
        String a = "aba";
        String b = "cdc";
        System.out.println(findLUSlength(a, b));
    }

    static int findLUSlength(String aString b) {
        if (a.equals(b))
            return -1;
        return Math.max(a.length(), b.length());
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

int findLUSlength(string astring b
{
        if(a==b)
              return -1;
        return max(a.size(),b.size());
}

int main()
{
    string  a = "aba"b = "cdc";
    cout<<findLUSlength(a,b);
    return 0;
}


No comments:

Post a Comment