Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Longest Uncommon Subsequence II problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Longest Uncommon Subsequence II problem solution we have Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.

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).

Longest Uncommon Subsequence II problem solution

Leetcode Longest Uncommon Subsequence II problem solution in Python.

def subseq(w1, w2):
    #True iff word1 is a subsequence of word2.
    i = 0
    for c in w2:
        if i < len(w1) and w1[i] == c:
            i += 1
    return i == len(w1)
    
A.sort(key = len, reverse = True)
for i, word1 in enumerate(A):
    if all(not subseq(word1, word2) 
            for j, word2 in enumerate(A) if i != j):
        return len(word1)
return -1

Longest Uncommon Subsequence II problem solution in Java.

class Solution {
    public int findLUSlength(String[] strs) {
        int res=-1, n=strs.length;
        for (int i=0; i<n; i++){
            if (strs[i].length()<res) continue;
            int j=-1;
            while(++j<n) if (i!=j && isSubsequence(strs[i], strs[j])) break;
            if (j==n) res=Math.max(res, strs[i].length());
        }
        return res;
    }
    public boolean isSubsequence(String a, String b){
        int i=0, j=0;
        while(i<a.length() && j<b.length()) if (a.charAt(i)==b.charAt(j++)) i++;
        return i==a.length();
    }
}

Problem solution in C++.

class Solution {
    static bool comp(string &a, string &b){
        return a.size() > b.size();
    }
    bool same(string &s1, string &s2)
    {
        int n = s1.size(), j = 0;
        for(int i = 0; i < n; i++)
        {
            if(s1[i] == s2[j])
            {
                j++;
                if(j == s2.size())
                    return true;
            }
        }
        return false;
    }
    bool iscommon(vector<string>& strs, string s, int idx)
    {
        int n = strs.size(), m = s.size();
        for(int i = 0; i < n && strs[i].size() >= m; i++)
        {
            if(idx == i) continue;
            if(same(strs[i], s))
                return false;
        }
        return true;
    }
public:
    int findLUSlength(vector<string>& strs) {
        sort(strs.begin(), strs.end(), comp);
        int n = strs.size();
        for(int i = 0; i < n; i++)
        {
            if(iscommon(strs, strs[i], i))
                return strs[i].size();
        }
        return -1;
    }
};

Problem solution in C.

int max(int a,int b)
{
    return a>b?a:b;
}

bool LCS(char* a, char* b) {
        if (strlen(a)<strlen(b)) return false;
        int i = 0;
        for(int k=0;k<strlen(a);k++) {
            if(i < strlen(b) && b[i] == a[k]) i++;
        }
        return i == strlen(b);
}

int findLUSlength(char ** strs, int strsSize){
    int ret=-1,i,j;

    for(i=0;i<strsSize;i++)
    {
        for(j=0;j<strsSize;j++)
        {
            if(i==j)
                continue;
            if(LCS(strs[j],strs[i]))
                break;
        }
        if(j==strsSize)
            ret=max(ret,strlen(strs[i]));
    }
    return ret;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes