首页 > 其他分享 >[LeetCode] 2486. Append Characters to String to Make Subsequence

[LeetCode] 2486. Append Characters to String to Make Subsequence

时间:2023-07-19 22:34:10浏览次数:48  
标签:end String 2486 Make int subsequence characters Append

You are given two strings s and t consisting of only lowercase English letters.

Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.

subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. 

Example 1:

Input: s = "coaching", t = "coding"
Output: 4
Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
Now, t is a subsequence of s ("coachingding").
It can be shown that appending any 3 characters to the end of s will never make t a subsequence.

Example 2:

Input: s = "abcde", t = "a"
Output: 0
Explanation: t is already a subsequence of s ("abcde").

Example 3:

Input: s = "z", t = "abcde"
Output: 5
Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
Now, t is a subsequence of s ("zabcde").
It can be shown that appending any 4 characters to the end of s will never make t a subsequence.

Constraints:

  • 1 <= s.length, t.length <= 105
  • s and t consist only of lowercase English letters.

追加字符以获得子序列。

给你两个仅由小写英文字母组成的字符串 s 和 t 。

现在需要通过向 s 末尾追加字符的方式使 t 变成 s 的一个 子序列 ,返回需要追加的最少字符数。

子序列是一个可以由其他字符串删除部分(或不删除)字符但不改变剩下字符顺序得到的字符串。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/append-characters-to-string-to-make-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是双指针。这是判断子序列的基本题,我们用两个指针 i, j 分别指向 s 和 t 并同时开始遍历。当 s 遍历完的时候,如果 t 也遍历完了,说明 t 本身就是 s 的子序列,否则需要补足的字母个数 = n - j。

时间O(m) - s 的长度

空间O(1)

Java实现

 1 class Solution {
 2     public int appendCharacters(String s, String t) {
 3         int m = s.length();
 4         int n = t.length();
 5         int i = 0;
 6         int j = 0;
 7         while (i < m && j < n) {
 8             if (s.charAt(i) == t.charAt(j)) {
 9                 j++;
10             }
11             i++;
12         }
13         if (j == n) {
14             return 0;
15         }
16         return n - j;
17     }
18 }

 

LeetCode 题目总结

标签:end,String,2486,Make,int,subsequence,characters,Append
From: https://www.cnblogs.com/cnoodle/p/17566956.html

相关文章

  • C# 后端请求 PostAsync GetStringAsync
     stringsendUrl=$"http://10.172.1.20/wtoptst/ws/r/awsp920";HttpClientsendclient=newHttpClient();stringtestStr=JsonConvert.SerializeObject(reques);//查看内容(测试用)HttpConten......
  • String
    一、概述 二、创建方式 三、内存模型 栈和方法有关堆和new(对象)有关方法区存放的是class形式的文件ps:JDK7以后有了一个StringTable(串池)专门在存储字符串;例:直接用双引号赋值优点:效率高,节省内存(因为如果该字符串在串池中已经存在,就不会再创建新的字符串,而是进行复用)......
  • MSSQL STRING_SPLIT(把字符串拆分成集合)
    语法:STRING_SPLIT(string,separator)参数说明:string:任何字符类型(例如nvarchar、varchar、nchar或char)的表达式separator:任何字符类型(例如nvarchar(1)、varchar(1)、nchar(1)或char(1))的单字符表达式,用作串联子字符串的分隔符根据字符把字符串拆分为集合S......
  • 数据库PostgreSQL PG 字符串拼接,大小写转换,substring
    前言PostgreSQL数据库简称pg数据库。本文主要介绍使用pg数据库时,字符串的一些常用操作。例如:多个字符串如何连接在一起,字符串如何大小写转换,删除字符串两边的空格,查找字符位置,查找子字符串等。一、多个字符串如何连接,拼接?pg的字符串连接使用||,注意不是+1.将2个字符串hello......
  • Makefile:162:recipe for target ‘xxx.o‘ failed!
    Q:使用makefile对工程进行编译的时候,出现指定报错:Makefile:162:recipefortarget‘xxx.o‘failed!A:该问题报错的意思是缺少依赖。根本问题是由于某个错误,导致过程文件xxx.o无法正常编译成功(makefile中最终目标文件的编译需要依赖过程目标文件xxx.o,而过程目标文件xxx.......
  • 深入解析 C++ 中的 ostringstream、istringstream 和 stringstream 用法
    引言:在C++中,ostringstream、istringstream和stringstream是三个非常有用的字符串流类,它们允许我们以流的方式处理字符串数据。本文将深入探讨这三个类的用法和特性,帮助读者更好地理解和应用字符串流操作。1.ostringstream(输出字符串流)ostringstream是C++中用于输出字......
  • spdlog日志库源码:CMake构建项目
    目录spdlog项目构成CMake构建根目录CMakeLists.txtcmake版本要求include专用cmake文件设置默认build类型编译器配置判断当前项目是否为spdlog选项开关spdlog项目构成Github源码:https://github.com/gabime/spdlogspdlog项目采用CMake构建,其一级目录结构如下$tree-L1.├─......
  • CMakeLists.txt 相关工作
    cmake_minimum_required(VERSION3.15)project(test)set(CMAKE_CXX_STANDARD11)set(SRCadd.cpptest.cppdiv.cpp)set(EXECUTABLE_OUTPUT_PATH/home/dabing/aa/bb/cc)add_executable(app${SRC})#搜索PROJECT_BINARY_DIR参数里的文件cpp.c,将文件存入SRC变量中,PROJE......
  • StringBuilder
    1publicclassdemo11{2publicstaticvoidmain(String[]args){3//StringBuilder可以看作为一个容器,创建之后里面内容可变4//1.创建对象5StringBuildersb=newStringBuilder();6//StringBuildersb=newStringBui......
  • StringJoiner
    JDK8出现的一个可变的操作字符串的容器,可以高效,方便的拼接字符串。在拼接的时候,可以指定间隔符号,开始符号,结束符号。1publicclassdemo15StringJoiner{2publicstaticvoidmain(String[]args){3int[]arr={1,2,3,4,5,};4StringJoine......