Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

521. Longest Uncommon Subsequence I

Easy61.0% Acceptance
String
Asked by:
G
Google
ProblemHints (3)Solutions (12)VideosCompanies (1)Notes

Problem Statement

Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If no such uncommon subsequence exists, return -1.

An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.

Example 1:

Input: a = "aba", b = "cdc"
Output: 3
Explanation: One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc".
Note that "cdc" is also a longest uncommon subsequence.

Example 2:

Input: a = "aaa", b = "bbb"
Output: 3
Explanation: The longest uncommon subsequences are "aaa" and "bbb".

Example 3:

Input: a = "aaa", b = "aaa"
Output: -1
Explanation: Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a. So the answer would be -1.

Constraints:

  • 1 <= a.length, b.length <= 100
  • a and b consist of lower-case English letters.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #521 Longest Uncommon Subsequence I, you are given two strings and must find the length of the longest subsequence that appears in one string but not in the other. The key observation is that if the two strings are exactly the same, then every subsequence of one string will also exist in the other, meaning there is no uncommon subsequence. In this case, the result is -1.

If the strings are different, the solution becomes straightforward. The longer string itself cannot be a subsequence of the shorter one, making it automatically an uncommon subsequence. Therefore, the answer is simply the maximum length of the two strings.

This approach relies on comparing the two strings and evaluating their lengths. Since we only perform a string comparison and basic length checks, the algorithm is efficient and avoids complex subsequence generation.

The overall complexity is linear with respect to the string length due to the equality comparison.

Complexity

ApproachTime ComplexitySpace Complexity
Compare strings and return max length if differentO(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Think very simple.

2
Hint 2

If <code>a == b</code>, the answer is -1.

3
Hint 3

Otherwise, the answer is the string <code>a</code> or the string <code>b</code>.

Ready to see the solutions?View Solutions

Solutions (12)

Direct String Comparison

This approach takes advantage of the fact that if two strings are identical, there are no uncommon subsequences. If they are different, the longest uncommon subsequence is the longest string itself.

  • If a is equal to b, return -1 because all subsequences of a are subsequences of b and vice versa.
  • If a is not equal to b, return the maximum length of a or b since the longer string itself cannot be a subsequence of the other.

Time Complexity: O(n) where n is the length of the strings, due to the string comparison.
Space Complexity: O(1), no additional space is needed.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4int findLUSlength(char * a, char * b) {
5    

Explanation

This C solution uses the standard library's strcmp function to check for string equality. If the strings are equal, it returns -1. Otherwise, it uses the strlen function to find the length of the longest string and returns that as the length of the longest uncommon subsequence.

Substring Verification

Another approach is to analyze the problem by considering the entire strings as potential subsequences and determine their existence in the other string.

  • Check if one string is a subsequence of the other and vice versa.
  • If neither string is a subsequence of the other, return the length of the longest string.
  • If one is a subsequence of the other, then no uncommon subsequence exists, return -1.

Time Complexity: O(n + m) where n and m are the lengths of the strings.
Space Complexity: O(1), since no additional structures are employed.

CC++JavaPythonC#JavaScript
1


Video Solutions

Watch expert explanations and walkthroughs

Longest Common Subsequence - Dynamic Programming - Leetcode 1143

NeetCode
18:25323,891 views

Asked By Companies

1 companies
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Substring Without Repeating CharactersMedium
Longest Palindromic SubstringMedium
Zigzag ConversionMedium
String to Integer (atoi)Medium
More similar problems

Related Topics

String

Problem Stats

Acceptance Rate61.0%
DifficultyEasy
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Longest Uncommon Subsequence I asked in coding interviews?

Yes, this problem or its variations can appear in coding interviews, especially as an introductory string problem. It tests a candidate's ability to identify simple observations and avoid overcomplicating the solution.

What data structure is best for Longest Uncommon Subsequence I?

No special data structure is required for this problem. A direct comparison of the two input strings and checking their lengths is sufficient to determine the result efficiently.

What is the optimal approach for Longest Uncommon Subsequence I?

The optimal approach is based on a simple observation. If both strings are identical, there is no uncommon subsequence, so the answer is -1. If they differ, the longer string itself cannot be a subsequence of the other, so the answer is the maximum length of the two strings.

Why does returning the longer string length work in this problem?

If two strings are different, the longer string cannot fully appear as a subsequence inside the shorter string. That guarantees it is an uncommon subsequence, making its length the correct answer.

return
strcmp
(
a
,
b
)
==
0
?
-
1
:
(
strlen
(
a
)
>
strlen
(
b
)
?
strlen
(
a
)
:
strlen
(
b
)
)
;
6
}
7
8
int
main
(
)
{
9
char
a
[
]
=
"aba"
;
10
char
b
[
]
=
"cdc"
;
11
printf
(
"%d\n"
,
findLUSlength
(
a
,
b
)
)
;
12
return
0
;
13
}
#
include
<stdio.h>
2
#
include
<string.h>
3
4
int
isSubsequence
(
char
*
s
,
char
*
t
)
{
5
int
i
=
0
,
j
=
0
;
6
while
(
i
<
strlen
(
s
)
&&
j
<
strlen
(
t
)
)
{
7
if
(
s
[
i
]
==
t
[
j
]
)
i
++
;
8
j
++
;
9
}
10
return
i
==
strlen
(
s
)
;
11
}
12
13
int
findLUSlength
(
char
*
a
,
char
*
b
)
{
14
if
(
isSubsequence
(
a
,
b
)
||
isSubsequence
(
b
,
a
)
)
return
-
1
;
15
return
strlen
(
a
)
>
strlen
(
b
)
?
strlen
(
a
)
:
strlen
(
b
)
;
16
}
17
18
int
main
(
)
{
19
char
a
[
]
=
"aba"
;
20
char
b
[
]
=
"cdc"
;
21
printf
(
"%d\n"
,
findLUSlength
(
a
,
b
)
)
;
22
return
0
;
23
}

Explanation

This solution defines a helper function isSubsequence that checks if one string is a subsequence of another using two pointers. It then uses this helper in findLUSlength to check the relations between a and b.