Leetcode: Shortest Way to Form String
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的更多相关文章
- [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 ... 
- 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 ... 
- 【回文】leetcode - Shortest Palindrome
		题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ... 
- [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 ... 
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
		[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ... 
- [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 ... 
- [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 ... 
- [LeetCode] Shortest Word Distance 最短单词距离
		Given a list of words and two words word1 and word2, return the shortest distance between these two ... 
- [LeetCode] Shortest Palindrome 最短回文串
		Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ... 
随机推荐
- ntohs的一个简单实现(将网络流中用两个字节16进制表示的资源数(如DNS)和长度转换为整形)
			我们知道在由于大端机和小端机导致网络字节序和主机序有可能是有差异的,我们可以使用系统的ntohs,ntohl,htons和htonl这些处理函数进行转换,下面是我写的一个关于ntohs在处理小端机字节 ... 
- DNS BIND配置 配置基本缓存服务器 DNS正向解析 DNS反向解析
			一. 缓存服务器配置 1.DNS:BIND Berkeley Internet Name Domain 版本bind97: RPM服务器端包的名字 安装bind-libs bind ... 
- LOJ#2764. 「JOI 2013 Final」JOIOI 塔
			题目地址 https://loj.ac/problem/2764 题解 真的想不到二分...不看tag的话... 考虑二分答案转化为判定问题,那么问题就变成了能不能组合出x个JOI/IOI,考虑贪心判 ... 
- 创建型模式(四) 建造者\生成器模式(Builder)
			一.动机(Motivation) 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们 ... 
- The 2018 ACM-ICPC CCPC 宁夏 A-Maximum Element In A Stack
			题意 对一个栈有入栈和出栈两种操作,求每次操作后栈的最大值的异或. 题目链接 分析 类似于单调栈,但是还没有那么复杂. 只需保持栈顶为最大值,如果入栈元素小于栈顶元素,则重复一次栈顶元素入栈:否则,直 ... 
- Tensorflow细节-P170-图像数据预处理
			由于6.5中提出的TFRecord非常复杂,可扩展性差,所以本节换一种方式 import tensorflow as tf from tensorflow.examples.tutorials.mni ... 
- 引领开发工具近40年的程序员Anders Hejlsberg
			有位神级程序员在近40年中一直创造引领潮流的开发工具(Turbo Pascal/Delphi/C#/TypeScript),他就是Anders Hejlsberg. 一. Anders并没有大学文凭, ... 
- 【转】根据Quartz-Cron表达式获取最近几次执行时间
			public static List<String> getRecentTriggerTime(String cron) { List<String> list = new A ... 
- [matlab工具箱] 神经网络Neural Net
			//目的是学习在BP神经网络的基础上添加遗传算法,蚁群算法等优化算法来优化网络,这是后话. 先简单了解了MATLAB中的神经网络工具箱,工具箱功能还是非常强大的,已经可以拟合出非常多的曲线来分析了. ... 
- 题解 UVA11105 【H-半素数 Semi-prime H-numbers】
			做这道题之前首先要掌握的是线性筛的模板 附上题目链接 https://www.luogu.org/problem/P3383 首先这道题目的范围有些特殊必须是% 4 == 1的数才行 所以可以这样 直 ... 
