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. 读书笔记 - javascript 高级程序设计 - 第二章 在Html中使用JavaScript

    1 <script>的6个属性 async  立即下载当前script标签的外部脚本 但不能影响别的 charset 没用了 defer  文档显示之后再执行脚本,只对外部脚本有效 lan ...

  2. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  3. 谈IO中的阻塞和非阻塞,同步和异步及三种IO模型

    什么是同步和异步? 烧水,我们都是通过热水壶来烧水的.在很久之前,科技还没有这么发达的时候,如果我们要烧水,需要把水壶放到火炉上,我们通过观察水壶内的水的沸腾程度来判断水有没有烧开.随着科技的发展,现 ...

  4. Python中使用print打印进度条

    import time for i in range(0,101,2): time.sleep(0.1) char_num = i//2 #打印多少个'*' per_str = '\r%s%% : % ...

  5. SQL基础教程(第2版)第2章 查询基础:练习题

    SELECT product_name, regist_date FROM Product WHERE regist_date > '2009-04-28'; ① ~ ③中的 SQL 语句都无法 ...

  6. 35. docker swarm dockerStack 部署 投票应用

    1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...

  7. 完整注册登陆php源码,附带session验证。

    1.首先先写表单页面login.html. <!DOCTYPE html> <html lang="en"> <head>     <me ...

  8. 损失函数coding

    损失函数(Loss Function)和成本函数(Cost Function)之间有什么区别? 在此强调这一点,尽管成本函数和损失函数是同义词并且可以互换使用,但它们是不同的. 损失函数用于单个训练样 ...

  9. react-native屏幕适配

    写一个屏幕适配类文件AdapterUtil.js,这样避免每次进行单位换算 "use strict" import {Dimensions, StatusBar, Platform ...

  10. springboot配置多个yml文件

    新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...