[LC] 1055. Shortest Way to Form String
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.
class Solution {
public int shortestWay(String source, String target) {
int res = 0;
int i = 0;
while (i < target.length()) {
int next = scan(source, target, i);
if (i == next) {
return -1;
}
i = next;
res += 1;
}
return res;
}
private int scan(String source, String target, int i) {
for (char c: source.toCharArray()) {
if (i != target.length()) {
char cur = target.charAt(i);
if (c == cur) {
i += 1;
}
}
}
return i;
}
}
[LC] 1055. Shortest Way to Form String的更多相关文章
- Leetcode: Shortest Way to Form String
From any string, we can form a subsequence of that string by deleting some number of characters (pos ...
- odoo 之报date<form string=''product lc''> 错误
原因是: </page> </notebook> </form> <div class="oe_chatter"> <fiel ...
- LC 934. Shortest Bridge
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- LC 245. Shortest Word Distance III 【lock, medium】
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LC] 243. Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- [LC] 151. Reverse Words in a String
Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" ...
随机推荐
- VUE注册全局组件和局部组件
全局组件 第一步:在components文件夹下建立一个子文件Users.vue <template> <div class="users"> {{msg} ...
- python numpy 矩阵左右翻转/上下翻转
numpy API: flattened flip() (in module numpy) fliplr() (in module numpy) flipud() (in module numpy) ...
- css 居中布局方案
position(transform css3 有些浏览器不兼容) <article id="one"> <section id="section&q ...
- Linux学习-课后练习(第二章命令)20200216
- openstack trove mongodb配置项
systemLog.verbosity 组件的默认日志消息详细程度级别. 详细程度级别决定MongoDB输出的信息和调试消息量. 详细级别可以在0到5之间: 0是MongoDB的默认日志详细程度级别, ...
- 洛谷 P1964 【mc生存】卖东西(多重背包)
题目传送门 解题思路: 题目里有,多重背包. AC代码: #include<iostream> #include<cstdio> #include<map> usi ...
- dd if= of= MBR
1.备份分区表信息 sudo fdisk -l >hda.txt #分区表信息重定向输出到文件中 2.备份MBR sudo dd if=/dev/sda of=mbr bs=512 count ...
- bitcoin-cli
== Blockchain ==getbestblockhashgetblock "blockhash" ( verbosity ) getblockchaininfogetblo ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL ALTER命令
需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. root@host# mysql -u root -p password; Enter password:******* ...
- Linux(CENTOS7) RabbitMq安装
RabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...