Longest Uncommon Subsequence I

LeetCode Q 521 - Longest Uncommon Subsequence I

Given a group of two strings, find the longest uncommon subsequence of this group of two strings.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Solution

Algorithm:
It is straitforward that we can divide this question into two cases:

  • Two input strings equal, then return -1;
  • Two input strings differ, then return the length of the longer one.

Code:

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

   Reprint policy


《Longest Uncommon Subsequence I》 by Tong is licensed under a Creative Commons Attribution 4.0 International License
  TOC