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. WEB监控手段

    WEB监控手段: 1.端口       本地: ss . netstat .lsof         ss -ntlp|grep 80|wc -l    (大于等于1)          netsta ...

  2. selenium 全天课整理(二)

    # encoding=utf-8 ''' selenium 全天 二 ''' #unittest例子 from selenium import webdriver import unittest,ti ...

  3. Python开发笔记之-字符串函数

    1.首字母大写 >>> s = 'yuanzhumuban' >>> s.capitalize() 'yuanzhumuban'  2.replace,替换 > ...

  4. 在eclipse运行一个项目报端口被占的问题

    1.端口被占问题解决方法. 我们运行javaweb项目的时候,如果不幸你的项目出现了上图的那种情况,不要慌,仅仅是端口被占了而已,只需要打开你tomcat里面的bin里面的shutdown.bat即可 ...

  5. Spring Cloud 组件 —— hystrix

    作用与功能 ① 资源隔离, 每个依赖配备单独的线程池,为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间.② 依赖超时,可配置依赖调用超时时间,超 ...

  6. git基础问题

    1).git add 与gitstage的区别 git stage只是git add的同义词,所以在使用上没有区别 i)Git仓库的三个组成部分:工作区(Working Directory).暂存区( ...

  7. windows下面同时部署多个tomcat的方法

    下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例)1. 下载apache-tomcat-7.0.63,下载下来的文件为apache-tomcat-7.0.63.zip.2. ...

  8. HDFS节点及原理

    HDFS节点角色: (1)namenode:1.用来存储HDFS的元数据信息,这里的元数据信息指的是文件系统的命名空间.启动时,将这些信息加载到namenode内存. 2.元数据信息也会在磁盘上保存成 ...

  9. Spring入门(四)——整合Mybatis

    1. 准备jar包及目录结构 2. 配置db.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/H ...

  10. Ural1297 最长回文子串(后缀数组+RMQ)

    /* 源程序丢失QWQ. 就不粘代码了. 大体做法是把串反转然后连接. 做一遍后缀数组. 对height做一遍rmq. 然后对于每个位置的奇偶分别判断, 记下pos. 注意求的是[l+1,r]的hei ...