代码拉取完成,页面将自动刷新
From any string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions).
Given two strings source and target, return the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, return -1.
Example 1:
Input: source = "abc", target = "abcbc"
Output: 2
Explanation: The target "abcbc" can be formed by "abc" and "bc", which are subsequences of source "abc".
Example 2:
Input: source = "abc", target = "acdbc"
Output: -1
Explanation: The target string cannot be constructed from the subsequences of source string due to the character "d" in target string.
Example 3:
Input: source = "xyz", target = "xzyxz"
Output: 3
Explanation: The target string can be constructed as follows "xz" + "y" + "xz".
Constraints:
Both the source and target strings consist of only lowercase English letters from "a"-"z".
The lengths of source and target string are between 1 and 1000.
class Solution {
public:
int shortestWay(string s, string t) {
int res=1;
unordered_set<char>hash;
for(char &c:s){
hash.insert(c);
}
for(char &c:t){
if(hash.find(c)==hash.end())return -1;
}
int index=0;
for(int i=0;i<t.size();i++){
bool found=false;
while(index<s.size()){
if(s[index]==t[i]){
found=true;
index++;
break;
}else{
index++;
}
}
if(found)continue;
res++;
index=0;
i--;
}
return res;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。