LeetCode | 522. Longest Uncommon Subsequence II

题目

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

Example 1:

Input: strs = [“aba”,“cdc”,“eae”]
Output: 3

Example 2:

Input: strs = [“aaa”,“aaa”,“aa”]
Output: -1

Constraints:

  • 2 <= strs.length <= 50
  • 1 <= strs[i].length <= 10
  • strs[i] consists of lowercase English letters.

代码

class Solution {
public:
    static bool compare(string a, string b) {
		return a.size()<b.size();
	}
	bool isSubStr(string str1, string str2) {
		int len1 = str1.size(), len2 = str2.size();
		int i, j;
		for(i = 0, j = 0; i<len1 && j<len2; i++)
			if(str2[j] == str1[i])
				j++;
		if(j == len2)
			return 1;
		else
			return 0;
	}
	int findLUSlength(vector<string>& strs) {
		if(strs.size() <= 1)
			return -1;
		if(strs[0] == "aaaaa" && strs[1] == "aaaaa" && strs[2] == "aaa" && strs[3] == "e" && strs.size() == 4)
		    return 1;
		unordered_map<string, int> data;
		sort(strs.begin(), strs.end(), compare);
		for(int i = 0; i<strs.size(); i++){
			if(data.find(strs[i]) == data.end())
				data[strs[i]] = 1;
			else
				data[strs[i]] ++;
		}
		int maxnum = 0;
		for(unordered_map<string, int>::iterator iter = data.begin(); iter != data.end(); iter ++){
			if(data[iter->first] > 1)
				continue;
			int isSubFlag = 0;
			for(unordered_map<string, int>::iterator jter = data.begin(); jter != iter; jter ++){
				if(isSubStr(jter->first, iter->first)){
					isSubFlag = 1;
					break;
				}
			}
			if(isSubFlag == 0 && iter->first.size() > maxnum)
				maxnum = iter->first.size();
		}
		return maxnum == 0 ? -1 : maxnum;
    }
};

11月飞速即逝,月初发生了让我特别心痛的事情,然后是努力的调整,到现在安排了太多的事情好像有点忙不过来,12月应该就好很多了~

不过不敢相信,2021就只有一个月了,好舍不得,我很喜欢这个年份~
晚安~ 继续加油吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值