Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.



A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.



The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.


Example 1:

Input: "aba", "cdc", "eae"
Output: 3



Note:

All the given strings' lengths will not exceed 10.
The length of the given list will be in the range of [2, 50].

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Sorting

Amortized analysis

Starter code: class Solution:
    def findLUSlength(self, strs: List[str]) -> int:
        

Solutions:
class Solution:

	def findLUSlength(self, strs):

		def isSubseq(s1, s2):
			(i, m) = (0, len(s1))
			for c in s2:
				if i == m:
					return True
				if s1[i] == c:
					i += 1
			return i == m
		strs.sort(key=len, reverse=True)
		for (i, s1) in enumerate(strs):
			if all((not isSubseq(s1, s2) for (j, s2) in enumerate(strs) if i != j)):
				return len(s1)
		return -1


class Solution:

	def findLUSlength(self, strs):

		def isSub(s1, s2):
			it = iter(s2)
			return all((i in it for i in s1))
		(keep, max1, N) = (False, -1, len(strs))
		mask = [True] * N
		for i in range(N):
			for j in range(N):
				if i != j and isSub(strs[i], strs[j]):
					mask[i] = False
					break
		for i in range(N):
			if mask[i]:
				max1 = max(max1, len(strs[i]))
		return max1


class Solution:

	def findLUSlength(self, strs):

		def check(a, b):
			i = 0
			for ai in a:
				while i < len(b) and ai != b[i]:
					i += 1
				if i == len(b):
					return False
				else:
					i += 1
			return True
		ans = -1
		for (i, a) in enumerate(strs):
			for (j, b) in enumerate(strs):
				if i != j and check(a, b):
					break
			else:
				ans = max(ans, len(a))
		return ans


class Solution:

	def findLUSlength(self, A):

		def subseq(w1, w2):
			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


class Solution:

	def findLUSlength(self, A):

		def issubsequence(s, t):
			t = iter(t)
			return all((c in t for c in s))
		for s in sorted(A, key=len, reverse=True):
			if sum((issubsequence(s, t) for t in A)) == 1:
				return len(s)
		return -1


class Solution:

	def findLUSlength(self, strs):

		def issub(w1, w2):
			i = 0
			for w in w2:
				if i < len(w1) and w1[i] == w:
					i += 1
			return i == len(w1)
		strs.sort(key=len, reverse=True)
		for s in strs:
			strs_copy = strs.copy()
			strs_copy.remove(s)
			if all((not issub(s, ss) for ss in strs_copy)):
				return len(s)
		return -1


class Solution:

	def findLUSlength(self, strs):

		def isSubseq(s1, s2):
			s2_it = iter(s2)
			return all((i in s2_it for i in s1))
		for k in sorted(strs, key=len, reverse=True):
			if sum((isSubseq(k, k2) for k2 in strs)) == 1:
				return len(k)
		return -1


class Solution:

	def findLUSlength(self, strs):
		answer = -1
		for i in range(len(strs)):
			x = strs[i]
			if len(x) > answer and all([not self.isSubsequence(x, strs[j]) for j in range(len(strs)) if i != j]):
				answer = len(x)
		return answer

	def isSubsequence(self, sub, sup):
		i = j = 0
		while i < len(sub) and j < len(sup):
			if sub[i] == sup[j]:
				i += 1
			j += 1
		return i == len(sub)


class Solution:

	def findLUSlength(self, strs):

		def is_subsequence(s, substr):
			i = 0
			j = 0
			while i < len(s) and j < len(substr):
				if s[i] == substr[j]:
					i += 1
					j += 1
				else:
					i += 1
			return j == len(substr)
		res = -1
		for i in range(len(strs)):
			j = 0
			while j < len(strs):
				if i == j:
					j += 1
					continue
				if is_subsequence(strs[j], strs[i]):
					break
				j += 1
			if j == len(strs):
				res = max(res, len(strs[i]))
		return res


class Solution:

	def findLUSlength(self, strs):

		def isSubsequence(a, b):
			i = 0
			j = 0
			while i < len(a) and j < len(b):
				if j == len(b) - 1 and (i < len(a) - 1 or a[i] != b[j]):
					return False
				if a[i] != b[j]:
					j += 1
				else:
					i += 1
					j += 1
			return True
		flag = 0
		l = []
		for i in range(len(strs)):
			for j in range(len(strs)):
				if i != j and isSubsequence(strs[i], strs[j]):
					flag = 1
					break
			if flag == 0:
				l.append(strs[i])
			flag = 0
		if l == []:
			return -1
		res = max(list([len(x) for x in l]))
		return res


class Solution:

	def findLUSlength(self, strs):

		def is_subseq(a, b):
			if len(b) > len(a):
				return False
			i = 0
			for c in a:
				if i < len(b) and b[i] == c:
					i += 1
			return i == len(b)
		strs.sort(key=lambda x: -len(x))
		for (i, s) in enumerate(strs):
			if not any((is_subseq(s2, s) for s2 in strs[:i])) and (not any((is_subseq(s2, s) for s2 in strs[i + 1:]))):
				return len(s)
		return -1