首页 > 其他分享 >LeetCode 2109. Adding Spaces to a String

LeetCode 2109. Adding Spaces to a String

时间:2024-04-01 10:57:12浏览次数:15  
标签:Adding string length indices 2109 spaces ind Spaces before

原题链接在这里:https://leetcode.com/problems/adding-spaces-to-a-string/description/

题目:

You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.

  • For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y' and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".

Return the modified string after the spaces have been added. 

Example 1:

Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
Output: "Leetcode Helps Me Learn"
Explanation: 
The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn".
We then place spaces before those characters.

Example 2:

Input: s = "icodeinpython", spaces = [1,5,7,9]
Output: "i code in py thon"
Explanation:
The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython".
We then place spaces before those characters.

Example 3:

Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
Output: " s p a c i n g"
Explanation:
We are also able to place spaces before the first character of the string.

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consists only of lowercase and uppercase English letters.
  • 1 <= spaces.length <= 3 * 105
  • 0 <= spaces[i] <= s.length - 1
  • All the values of spaces are strictly increasing.

题解:

Traversae the string from left to right, when index == spaces[ind], then add a space to res StringBuilder and ind++. 

Note: when checking spaces[ind], check ind < spaces.length first.

Time Complexity: O(n + m). n = s.length(). m = spaces.length.

Space: O(n + m)

AC Java:

 1 class Solution {
 2     public String addSpaces(String s, int[] spaces) {
 3         if(s == null || spaces == null || spaces.length == 0){
 4             return s;
 5         }
 6 
 7         StringBuilder sb = new StringBuilder();
 8         int ind = 0;
 9         for(int i = 0; i < s.length(); i++){
10             if(ind < spaces.length && i == spaces[ind]){
11                 sb.append(" ");
12                 ind++;
13             }
14 
15             sb.append(s.charAt(i));
16         }
17 
18         return sb.toString();
19     }
20 }

 

标签:Adding,string,length,indices,2109,spaces,ind,Spaces,before
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18107933

相关文章

  • yarn workspaces focus @my-org/app 解释这段
    yarnworkspacesfocus@my-org/app解释这段yarnworkspacesfocus@my-org/app是YarnWorkspaces的一个命令,这个命令允许你在具有多个工作区(workspace)的monorepo(单体仓库)项目中集中注意力和资源只在一个特定的工作区上执行任务,如安装依赖、运行脚本等。具体解释:YarnWorks......
  • 在使用 Yarn Workspaces 时,有业务包A和组件库包B,两个包分别引用了antd4 和antd5 三方
    在使用YarnWorkspaces时,有业务包A和组件库包B,两个包分别引用了antd4和antd5三方包,安装依赖时该注意什么?在使用YarnWorkspaces的项目中,如果有业务包A和组件库包B,分别引用了antdv4和antdv5两个不同版本的三方包,安装依赖时需要注意以下几点:版本隔离:由于antd......
  • `ij_javascript_spaces_within_imports = true` 这个设置表示在 JavaScript 代码的
    #http://editorconfig.orgroot=true[*]#表示所有文件适用charset=utf-8#设置文件字符集为utf-8indent_style=tab#缩进风格(tab|space)indent_size=4#缩进大小end_of_line=lf#控制换行类型(lf|cr|crlf)trim_trailing_whitespace=true#去除......
  • innodb_undo_tablespaces导致Mysql启动报错
    1.问题MySQL5.7设置innodb_undo_tablespaces=2报错如下:2020-06-09T04:40:07.800321-05:000[ERROR]InnoDB:Expectedtoopen2undotablespacesbutwasabletofindonly0undotablespaces.Settheinnodb_undo_tablespacesparametertothecorrectvalueandret......
  • 使用 openssl 进行 RSA/ECB/PKCS1PADDING 加解密
    使用java进行RSA/ECB/PKCS1PADDING是非常方便的,例如下面的示例publicstaticStringpublicDecrypt(PublicKeypublicKey,Stringencrypted)throwsException{Ciphercipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");cipher.init(Cipher.DECRYPT_......
  • Coursera自然语言处理专项课程01:Natural Language Processing with Classification an
    NaturalLanguageProcessingwithClassificationandVectorSpacesCourseCertificate本文是NaturalLanguageProcessingwithClassificationandVectorSpaces这门课的学习笔记,仅供个人学习使用,如有侵权,请联系删除。文章目录NaturalLanguageProcessingwi......
  • 5-adding_general_force
    D’ALEMBERT’SPRINCIPLEForparticlesD’Alembert’sprincipleimpliesthat,ifwehaveasetofforcesactingonanobject,wecanreplaceallthoseforceswithasingleforce,whichiscalculatedby\[f=\sum\limits_{i}f_{i}\]Inotherwords,wes......
  • CF1312C Adding Powers 题解
    题意:对于一个初始全\(0\)的序列,问是否能够进行若干次操作(第\(i\)次操作为对序列中任意一个元素增加\(k^i\)),使得此序列变为目标数组\(a\)。首先,我们令需要进行操作的序列为\(b\)。我们知道,如果能通过若干次操作将\(b\)变为\(a\),则有以下三种情形:\(a\)中的元素全......
  • from Crypto.Util.Padding import pad,unpad 报错,没有找到依赖
    1、安装pipinstallpycryptodomepipinstallCrypto2、安装完成后重启idea,发现还是没有打开依赖包所在的文件夹:安装位置\Lib\site-packages发现Crypto是小写,将代码中的引入改成小写fromcrypto.Util.Paddingimportpad,unpad 3、打开crypto文件夹,看到Util和Ciph......
  • 前端 html 一个元素padding-right,不起作用?毫无反应?padding right 无效
    有没有宝子,开发html,给一个父元素padding-right,子元素却毫无反应,万分捉急,在线等,急!我知道你着急,但是你先别急我会在这里娓娓道来,带你走上一个新的技术台阶1、一段基础代码代码如下:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"......