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的更多相关文章

  1. Leetcode: Shortest Way to Form String

    From any string, we can form a subsequence of that string by deleting some number of characters (pos ...

  2. odoo 之报date<form string=''product lc''> 错误

    原因是: </page> </notebook> </form> <div class="oe_chatter"> <fiel ...

  3. LC 934. Shortest Bridge

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

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

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

  6. [LC] 244. Shortest Word Distance II

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  7. [LC] 243. Shortest Word Distance

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  9. [LC] 151. Reverse Words in a String

    Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" ...

随机推荐

  1. c# 用户控件,usercontrol,自定义控件属性

    1.C#用户控件的使用 2.拖动添加:画面上如需多个usercontrol,添加TableLayoutPanel,然后在工具箱中找到usercontrol,拖到相应框中 3.代码添加:主窗口中有多个T ...

  2. 解决fixed布局里内容不滚动问题

    //注意给父级加上 (1)横向滚动 left:0; right:0; 然后想要横向滚动的话 overflow-x:scroll; overflow-y:hidden; (2)竖直方向滚动top:0;b ...

  3. 洛谷 P1220 关路灯(区间dp,前缀和)

    传送门 解题思路 先明确一下题意,c指的是路灯的编号而不是位置. 然后根据贪心,在从点i去关点j的路灯时,所有经过的路灯都会随手关掉(不耗时间),所以我们可以确定,若i点和j点的路灯已经关闭,那么区间 ...

  4. Day 8:方法上自定义泛型、类上、接口上、泛型的上下限

    泛型 泛型是jdk1.5使用的新特性  泛型的好处:   1. 将运行时的异常提前至了编译时    2. 避免了无谓的强制类型转换 泛型在集合中的常见应用:    ArrayList<Strin ...

  5. Arduino Wireless Communication – NRF24L01 Tutorial(arduino无线通信---NRF24L01教程)

    arduino下nrf24l01库文件及相关说明 库的说明文档 https://tmrh20.github.io/RF24/ 库的源代码github下载页面 https://tmrh20.github ...

  6. 线段树--线段树【模板1】P3372

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入格式 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含 ...

  7. D语言-运算符

    Part 0:概念 表达式:表达式是由非赋值运算符或特殊运算符和值组成的,每个表达式都可以计算出一个值 Part 1:非赋值运算符 §1.1 基本的运算符 基本的运算符有+,-,*,/,% 我相信你除 ...

  8. Odd sum (对本菜鸡来说是个极坑题)

    https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...

  9. MQTT的编译和安装(mosquitto)

    1.基于IBM开发的开元框架实现mosquitto  下载地址:http://mosquitto.org/files/source/ 编译安装:(参考链接:https://www.cnblogs.co ...

  10. zabbix使用短信猫实现报警

    因为公司运维的对象是政府单位,所以在实际的监控过程中无法连接到外网,所以最后报警选择的媒介是短信猫,下边就是具体的实施过程. 一.面临的问题 因为手头上的设备是串口的短信猫,但是zabbix serv ...