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.

Greedy

Use two pointers, one for source: i, one for target: j. While j scan through target, try to match each char of j in source by moving i. Count how many times i goes through source end.

 class Solution {
public int shortestWay(String source, String target) {
char[] sc = source.toCharArray(), ta = target.toCharArray();
boolean[] map = new boolean[26];
for (char c : sc) {
map[c - 'a'] = true;
} int j = 0, res = 1;
for (int i = 0; i < ta.length; i ++, j ++) {
if (!map[ta[i] - 'a']) return -1;
while (j < sc.length && sc[j] != ta[i]) j ++;
if (j == sc.length) {
res ++;
j = -1;
i --;
}
}
return res;
}
}

Leetcode: Shortest Way to Form String的更多相关文章

  1. [LC] 1055. Shortest Way to Form String

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

  2. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  3. 【回文】leetcode - Shortest Palindrome

    题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  4. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  5. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  7. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  8. [LeetCode] Shortest Word Distance 最短单词距离

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

  9. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. 用chrome浏览器进行前端debug和停止debug

    首先F12打开控制台: 选择"source","Ctrl+Shift+F"搜索需要debug的代码关键词(Ctrl+O根据文件名搜索): 打开需要debug的文 ...

  2. java.lang.NoClassDefFoundError: org/apache/zookeeper/proto/SetWatches

    Session 0x16b21fa441900b6 for server 192.168.240.126/192.168.240.126:2181, unexpected error, closing ...

  3. vue 博客知识点汇总

    1. vue修改url,页面不刷新 项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一 ...

  4. docker的入门到放弃--docker基本命令

    docker的镜像中国:https://www.docker-cn.com/registry-mirror 1.搜索镜像 [root@localhost ~]# docker search tomca ...

  5. 行为型模式(九) 访问者模式(Visitor)

    一.动机(Motivate) 在软件构建过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法),如果直接在基类中做这样的更改,将会给子类带来很繁重的变更负担,甚至破坏原有设计.如何在不更 ...

  6. jdk、jre、jvm三者联系

    JDK(Java Development Kit)是针对Java开发员的产品,是整个Java的核心,包括了Java运行环境JRE.Java工具和Java基础类库.Java Runtime Enviro ...

  7. JS基础篇之作用域、执行上下文、this、闭包

    前言:JS 的作用域.执行上下文.this.闭包是老生常谈的话题,也是新手比较懵懂的知识点.当然即便你作为老手,也未必真的能理解透彻这些概念. 一.作用域和执行上下文 作用域: js中的作用域是词法作 ...

  8. URLSearchParams对象

    URLSearchParams对象用于处理URL中查询字符串,即?之后的部分. 1.语法 其实例对象的用法和Set数据结构类似.实例对象本身是可遍历对象.但是不是遍历器. var paramsStri ...

  9. 一.使用LDAP认证

    作用:网络用户认证,用户集中管理 网络用户信息:LDAP服务器提供 本地用户信息:/etc/passwd /etc/shadow提供     LDAP服务器:虚拟机classroom     LDAP ...

  10. make 的使用参数