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. mybatis的一对多和多对一的连接查询

    实体类: package com.entity; import java.util.List; public class Dept { private Integer deptId; private ...

  2. 一天一个设计模式——(Singleton)单例模式(线程安全性)

    一.模式说明 有时候,我们希望在应用程序中,仅生成某个类的一个实例,这时候需要用到单例模式. 二.模式类图 三.模式中的角色 Singleton角色,该模式中仅有的一个角色,该角色有一个返回唯一实例的 ...

  3. 多源D点(邻接表+bfs)

    [问题]给出一颗n个结点的树,树上每条边的边权都是1,这n个结点中有m个特殊点,请你求出树上距离这m个特殊点距离均不超过d的点的数量,包含特殊点本身. 输入: 输入第一行包含三个正整数,n.m.d分别 ...

  4. Java:面向对象的理解

    面向对象 一切皆对象.程序是对象的集合,它们通过发送消息来告知彼此所要做的.也就是说:以对象为中心,以消息(发送消息即为函数调用)为驱动.对象具有状态,行为和标识. 状态:指类的数据成员,即属性: 行 ...

  5. php curl模拟post请求提交数据例子总结

    php curl模拟post请求提交数据例子总结 [导读] 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考 ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM 事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]

    题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...

  9. rename 修改文件名

    Linux的 rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,早期的Linux发行版基本上使用的是C语言版本的,现在已经很难见到C语言版本的了,由于历史原因,在Perl语言 ...

  10. gcd--最大公因数

    求两个数的最大公倍数 考完五校的第一天,在家补视频ing,简单来说的话就是给了两个数A,B 假设他们两个的最大公倍数为d,那么A=X*d,B=Y*d gcd就是把一直gcd(B%A,A)不断更新,其中 ...