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. ...
随机推荐
- MySQL 空事务
问题描述; 研发同事反应MySQL数据库有锁,检查innodb_trx时,发现有很多长时间未结束的空事务. 这些事务的trx_mysql_thread_id都为0,因此不能通过kill ...
- 版本控制Git研究二
经过好几周的工作忙碌,终于可以有时间静下心来学习东西了,做为崇尚技术的人来说,应该是最幸福的时光了,呵呵,当然也是宅神的表现啦,话不多说,继续git的学习,上次已经对git的一些概念进行了说明,具体可 ...
- noi 7827 质数的和与积
描述 两个质数的和是S,它们的积最大是多少? 输入一个不大于10000的正整数S,为两个质数的和.输出一个整数,为两个质数的最大乘积.数据保证有解.样例输入 50 样例输出 589 欧拉筛+尺取.实际 ...
- DevExpress20:XtraCharts控件实现图表
一.总体概述 官方文档: https://docs.devexpress.com/WindowsForms/8117/controls-and-libraries/chart-control Char ...
- SpringBoot官方文档学习(二)Externalized Configuration(外部化配置)
Spring Boot允许您将配置外部化,以便可以在不同的环境中使用相同的应用程序代码.您可以使用属性文件.YAML文件.环境变量和命令行参数来具体化配置.属性值可以通过使用@Value注释直接注入b ...
- C# 线程小结
进程与线程 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行流,每个线 ...
- OPCode详解及汇编与反汇编原理
1. 何为OPCode 在计算机科学领域中,操作码(Operation Code, OPCode)被用于描述机器语言指令中,指定要执行某种操作的那部分机器码,构成OPCode的指令格式和规范由处理器的 ...
- 洛谷 P5461 赦免战俘 题解
P5461 赦免战俘 题目背景 借助反作弊系统,一些在月赛有抄袭作弊行为的选手被抓出来了! 题目描述 现有 \(2^n\times 2^n (n\le10)\) 名作弊者站成一个正方形方阵等候 kkk ...
- P1041 传染病控制——暴力遍历所有相同深度的节点
P1041 传染病控制 说实话这种暴力我还是头一次见,每次病毒都会往下传染一层: 数据范围小,我们可以直接枚举当前层保护谁就好了: 用vector 记录相同层数的节点:维护已经断了的点: 如果超出最底 ...
- LocalStorage 的 具体操作 与 设置有效期
读取与存储 的使用方法 //存储 (名称 , 内容 , 有效期<小时> ) // 如果想要自己改变 有效期的单位 可以在 set 方法中 // 找到 这两个 setHours getHou ...