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. ...
随机推荐
- 思考---(科研99% )VS (产品75%)
转目前人脸识别技术的挑战是什么? - 知乎 标签: | 发表时间:-- : | 作者: 出处:https://www.zhihu.com 也是放假太闲,上知乎来锻炼一下手指. 在回答题主的问题的时候, ...
- 小程序框架之视图层 View~基础组件
框架为开发者提供了一系列基础组件,开发者可以通过组合这些基础组件进行快速开发.详细介绍请参考组件文档. 什么是组件: 组件是视图层的基本组成单元. 组件自带一些功能与微信风格一致的样式. 一个组件通常 ...
- Spring4- 04-Spring简易整合Mybatis -导入jar包/ 正常编写pojo/ 编写spring 配置文件
笔记要点&出错分析与总结 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,工程组织 (AirportService为机场 ...
- [转]Linux网络 - 数据包的发送过程
转, 原文:https://segmentfault.com/a/1190000008926093 -------------------------------------------------- ...
- python 程序练习题
1.实现isOdd(),参数为整数,如果整数为奇数,返回True,否则返回Flase 代码如下: def isOdd(a): if a%2==0: return False else: return ...
- 《少年先疯队》第八次团队作业:Alpha冲刺第五天
前言 第五天冲刺会议 时间:2019.6.18 地点:宿舍 5.1 今日完成任务情况以及遇到的问题. 5.1.1今日完成任务情况 姚玉婷:会员查询消费记录功能的实现 马丽莎:昨天 ...
- python的种类有哪些?
CPython 当我们从Python官方网站下载并安装好Python 3.6后,我们就直接获得了一个官方版本的解释器:CPython.这个解释器是用C语言开发的,所以叫CPython.在命令行下运行p ...
- car二手车购买原理
前提须知 1. Kbb,指著名的二手车估计网站 www.kbb.com .其中给出了具体年份里程车的参考价格. 2. Craigslist,简称 CL,著名的二手物品交易网站,美国的 58 同城. 3 ...
- React vs Angular vs Vue 2019
React vs Angular vs Vue 看待这三个主流框架给出的想法 Angular is the entire kitchen that gives you all the tools ne ...
- Hive-2.3.6 安装
本安装依赖Haddop2.8安装 https://www.cnblogs.com/xibuhaohao/p/11772031.html 一.下载Hive与MySQL jdbc 连接驱动 apache- ...