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. ...
随机推荐
- opencv摄像头读取图片
# 摄像头捕获图像或视频import numpy as np import cv2 # 创建相机的对象 cap = cv2.VideoCapture(0) while(True): # 读取相机所拍到 ...
- ldconfig 让安装的 php 的rdkafka生效
原文:https://www.cnblogs.com/schips/p/10183111.html linux中ldconfig的使用介绍 ldconfig是一个动态链接库管理命令,其目的为了让动 ...
- .net框架-栈(Stack)
栈(Stack) 栈代表一个后进先出的集合 栈元素为Object类型 .net框架提供Stack<T>泛型栈类 压栈(Push)和出栈(Pop)是栈的基本操作,压栈入栈顶,出栈也出栈顶. ...
- html页面标记 点击目录跳转到页面相应位置 简易回到顶部
html页面标记 点击目录跳转到页面相应位置 简易回到顶部 参考博客:
- restful接口规范 | 基于restful的原生django接口
restful接口规范 接口 接口:联系两个物质的媒介,完成信息交互 web程序中:联系前台页面与后台数据库的媒介 web接口组成: - url:长得像返回数据的url链接 - 请求参数:前台按照指定 ...
- linux下新磁盘创建lvm、扩容lvm
1.首先查看磁盘fdisk -l2.进入磁盘fdisk /dev/sdbn 创建新磁盘p 创建主分区创建分区ID 1-4为主分区根据提示选择磁盘开始位置(默认空格就好)选择结束位置(新增磁盘大小)t ...
- c字符数组之两头堵模型
char *其实就是char[length]的首元素地址 实验环境:centos7下qt5.11 中文char类型占3个字节 char[length]="特别车队"其实等价于ch ...
- (尚007)Vue强制绑定class和style
注意:class和style的值是动态的值 1.test007.html <!DOCTYPE html><html lang="en"><head&g ...
- tar归档压缩命令和zip归档 和7zip压缩命令;库文件归档ar命令
第一.tar 归档 tar -c 创建归档文件包 tar -x 释放归档文件包 tar -t 查看归档文件包 tar -v 显示归档包操作过程信息 tar -f 指定归档文件名 案例1:归档 /hom ...
- 2019acm山东省赛C题
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4115 昨天赛场上只想到了一种情况:最远点一定是在最后一次循环中产生 ...