[LC] 1055. 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.
class Solution {
public int shortestWay(String source, String target) {
int res = 0;
int i = 0;
while (i < target.length()) {
int next = scan(source, target, i);
if (i == next) {
return -1;
}
i = next;
res += 1;
}
return res;
}
private int scan(String source, String target, int i) {
for (char c: source.toCharArray()) {
if (i != target.length()) {
char cur = target.charAt(i);
if (c == cur) {
i += 1;
}
}
}
return i;
}
}
[LC] 1055. Shortest Way to Form String的更多相关文章
- Leetcode: Shortest Way to Form String
From any string, we can form a subsequence of that string by deleting some number of characters (pos ...
- odoo 之报date<form string=''product lc''> 错误
原因是: </page> </notebook> </form> <div class="oe_chatter"> <fiel ...
- LC 934. Shortest Bridge
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- LC 245. Shortest Word Distance III 【lock, medium】
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LC] 243. Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- [LC] 151. Reverse Words in a String
Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" ...
随机推荐
- Sequence Models Week 1 Improvise a Jazz Solo with an LSTM Network
Improvise a Jazz Solo with an LSTM Network Welcome to your final programming assignment of this week ...
- pytorch学习问题汇总
问题六: 问题五:这里是怎么得到的? 问题四:为什么会是如下结果? torch.bernoulli(a)怎么是这个结果? 问题1:torch各个类型数据格式如何转换?数据类型在官方文档torch.Te ...
- PL/SQL 连接oracle步骤
下面就将PL/SQL的配置说明一下. 一.安装Oracle客户端,让后配置 安装目录下面的C:\ORACLE\instantclient_11_2\NETWORK\ADMIN 的 tnsname ...
- 教你如何使用JavaScript入门
JavaScript简介 JavaScript是NetScape公司为Navigator浏览器开发的,是web前端卸载HTML文件中的一种脚本语言,能实现网页内容的交互显示.当用户在客户端显示该网 ...
- (转)绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程
以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示: 图一 添加商品界面 系统的代码目录结构及add.jsp代码如图二所示: 图二 系统的代码目录结构及add.js ...
- tomcat8.5的安装、卸载、配置和部署
安装和卸载 下载 http://tomcat.apache.org/ 环境变量 1.点击此电脑 右键—>属性. 2.创建变量名为CATALINA_HOME,的值为所解压安装tomcat的本地目录 ...
- str_replace用法
语法 str_replace(find,replace,string,count) 参数 描述 find 必需.规定要查找的值. replace 必需.规定替换 find 中的值的值. string ...
- 前端Json 增加,删除,修改元素(包含json数组处理)
一:基础JSON对象 二:JSON数组数据 以下为增删修改方法: <!DOCTYPE html> <html lang="en"> <head> ...
- EL表达式和JSTL(一)
一. 初始JavaBean 在软件开发时,有些数据时经常要用到的,为了方便进行移植,Sun公司提出了一种JavaBean技术,使用JavaBean对这些数据进行封装,做到一次编写,到处开发. Java ...
- 吴裕雄--天生自然 JAVA开发学习:StringBuffer、数组
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(& ...