leetcode - 408. Valid Word Abbreviation

Description

A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.

For example, a string such as “substitution” could be abbreviated as (but not limited to):

"s10n" ("s ubstitutio n")
"sub4u4" ("sub stit u tion")
"12" ("substitution")
"su3i1u2on" ("su bst i t u ti on")
"substitution" (no substrings replaced)

The following are not valid abbreviations:

"s55n" ("s ubsti tutio n", the replaced substrings are adjacent)
"s010n" (has leading zeros)
"s0ubstitution" (replaces an empty substring)

Given a string word and an abbreviation abbr, return whether the string matches the given abbreviation.

A substring is a contiguous non-empty sequence of characters within a string.

Example 1:

Input: word = "internationalization", abbr = "i12iz4n"
Output: true
Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").

Example 2:

Input: word = "apple", abbr = "a2e"
Output: false
Explanation: The word "apple" cannot be abbreviated as "a2e".

Constraints:

1 <= word.length <= 20
word consists of only lowercase English letters.
1 <= abbr.length <= 10
abbr consists of lowercase English letters and digits.
All the integers in abbr will fit in a 32-bit integer.

Solution

Sweep the abbreviation, if it’s a number, continue. If it’s a character, compare.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

class Solution:def validWordAbbreviation(self, word: str, abbr: str) -> bool:word_index = 0num = 0for each_c in abbr:if each_c.isdigit():if num == 0 and each_c == '0':return Falsenum = num * 10 + int(each_c)else:word_index += numnum = 0if word_index >= len(word) or word[word_index] != each_c:return Falseword_index += 1word_index += numreturn word_index == len(word)

Version2

class Solution:def validWordAbbreviation(self, word: str, abbr: str) -> bool:# i: index for word, j: index for abbri, j = 0, 0num = 0while i < len(word) and j < len(abbr):if word[i] == abbr[j]:i += 1j += 1else:if not abbr[j].isdigit() or abbr[j] == '0':return Falsenum = 0while j < len(abbr) and '0' <= abbr[j] <= '9':num = num * 10 + int(abbr[j])j += 1i += numreturn i == len(word) and j == len(abbr)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/673854.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

详细分析python中的from waitress import serve(附Demo)

目录 前言1. 基本知识2. serve源码分析3. 基本操作 前言 以前玩python 开发的时候写过一些见解&#xff0c;推荐阅读&#xff1a; uwsgi启动django以及uwsgi.ini的配置参数详解Django框架零基础入门 部署服务器除了Flask还有serve 在讲述serve之前&#xff0c;先讲述两者的…

安全名词解析-社工、0day、DDos攻击

为方便您的阅读&#xff0c;可点击下方蓝色字体&#xff0c;进行跳转↓↓↓ 01 社工02 0day漏洞03 DDoS攻击 01 社工 社工&#xff08;Social Engineering&#xff09;&#xff0c;一般指社会工程攻击的简称&#xff0c;是一种通过与人的交互来获取信息、获取访问权限或进行欺骗…

shell 脚本输出java版本到日志文件中

要将Java版本输出到日志文件中&#xff0c;你可以使用Shell脚本调用java -version命令&#xff0c;并将输出重定向到一个日志文件中。下面是一个简单的示例脚本&#xff0c;展示了如何实现这一功能&#xff1a; #!/bin/bash# 指定日志文件路径 log_file"/path/to/your/log…

前端vue3实现本地及在线文件预览(含pdf/txt/mp3/mp4/docx/xlsx/pptx)

一、仅需实现在线预览&#xff0c;且文件地址公网可访问 &#xff08;一&#xff09;微软office免费预览&#xff08;推荐&#xff09; 支持doc/docx/xls/xlsx/ppt/pptx等多种office文件格式的免费预览 //示例代码//​在https://view.officeapps.live.com/op/view.aspx?src…

【Qt】Android上运行keeps stopping, Desktop上正常

文章目录 问题 & 背景背景问题 解决方案One More ThingTake Away 问题 & 背景 背景 在文章【Qt】最详细教程&#xff0c;如何从零配置Qt Android安卓环境中&#xff0c;我们在Qt中配置了安卓开发环境&#xff0c;并且能够正常运行。 但笔者在成功配置并完成上述文章…

(每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第10章 项目进度管理(三)

博主2023年11月通过了信息系统项目管理的考试&#xff0c;考试过程中发现考试的内容全部是教材中的内容&#xff0c;非常符合我学习的思路&#xff0c;因此博主想通过该平台把自己学习过程中的经验和教材博主认为重要的知识点分享给大家&#xff0c;希望更多的人能够通过考试&a…

2024/2/7总结

Node.js 什么是node.js node.js是一个基于chrome v8 引擎的 JavaScript 运行环境。 浏览器是JavaScript的前端运行环境node.js是JavaScript的后端运行环境 node.js中无法调用DOM和BOM等浏览器内置API fs模块 是node.js官方提供的、用来操作文件的模块&#xff0c;它提供了一系…

初识 Protobuf 和 gRpc

初步了解 Protobuf 和 gRpc Protocol Buffers Protocol Buffers&#xff08;又称protobuf&#xff09;是谷歌的语言无关、平台无关、可扩展的机制&#xff0c;用于序列化结构化数据。您可以在protobuf的文档中了解更多关于它的信息。 ProtoBuf 的定义 ProtoBuf是将类的定义…

PostgreSql与Postgis安装

POstgresql安装 1.登录官网 PostgreSQL: Linux downloads (Red Hat family) 2.选择版本 3.安装 ### 源 yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm ### 客户端 yum install postgresql14 ###…

Python使用zdppy_es国产框架操作Elasticsearch实现增删改查

Python使用zdppy_es国产框架操作Elasticsearch实现增删改查 本套教程配套有录播课程和私教课程&#xff0c;欢迎私信我。 Docker部署ElasticSearch7 创建基本容器 docker run -itd --name elasticsearch -p 9200:9200 -e "discovery.typesingle-node" -e ES_JAVA_…

获取目标进程导入DLL模块地址的方法

以下是一些常用的方法&#xff0c;可以用于获取目标进程导入DLL模块的地址。根据具体情况选择合适的方法&#xff0c;并注意安全性和稳定性&#xff1a; 1、使用 LoadLibrary 函数和 GetModuleHandle 函数&#xff1a; #include <Windows.h>// 获取目标进程中某个 DLL …

MySQL基础查询篇(10)-正则表达式的基础使用

正则表达式在MySQL数据库中是非常强大且常用的功能&#xff0c;它可以帮助我们快速的匹配和处理文本数据。在MySQL中&#xff0c;正则表达式的使用可以通过正则表达式函数来实现。 MySQL中常用的正则表达式函数有&#xff1a; REGEXP/RLIKE&#xff1a;通过正则表达式进行模式…

java Map集合相关介绍和方法使用操作

Java的Map集合是存储键值对的一种数据结构。在Map中&#xff0c;每个键都是唯一的&#xff0c;用来标识该键值对。 Map接口是java.util包中的一部分&#xff0c;它有多个实现类&#xff0c;包括HashMap&#xff0c;TreeMap&#xff0c;LinkedHashMap等。 下面是Map接口的一些…

Docker的镜像和容器的区别

1 Docker镜像 假设Linux内核是第0层&#xff0c;那么无论怎么运行Docker&#xff0c;它都是运行于内核层之上的。这个Docker镜像&#xff0c;是一个只读的镜像&#xff0c;位于第1层&#xff0c;它不能被修改或不能保存状态。 一个Docker镜像可以构建于另一个Docker镜像之上&…

P2957

题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how mu…

numa网卡绑定

#概念 参考&#xff1a;https://www.jianshu.com/p/0f3b39a125eb(opens new window) chip&#xff1a;芯片&#xff0c;一个cpu芯片上可以包含多个cpu core&#xff0c;比如四核&#xff0c;表示一个chip里4个core。 socket&#xff1a;芯片插槽&#xff0c;颗&#xff0c;跟…

Sping Cloud Hystrix 参数配置、简单使用、DashBoard

Sping Cloud Hystrix 文章目录 Sping Cloud Hystrix一、Hystrix 服务降级二、Hystrix使用示例三、OpenFeign Hystrix四、Hystrix参数HystrixCommand.Setter核心参数Command PropertiesFallback降级配置Circuit Breaker 熔断器配置Metrix 健康统计配置Request Context 相关参数C…

Docker容器监控-CIG

目录 一、CIG说明 1. CAdvisor 2. InfluxDB 3. Grafana 二、环境搭建 1. 创建目录 2. 编写 docker-compose.yml 3. 检查并运行容器 三、进行测试 1. 查看 influxdb 存储服务 是否能正常访问 2. 查看 cAdvisor 收集服务能否正常访问 3. 查看 grafana 展现服务&#…

金融行业专题|证券超融合架构转型与场景探索合集(2023版)

更新内容 更新 SmartX 超融合在证券行业的覆盖范围、部署规模与应用场景。新增操作系统信创转型、Nutanix 国产化替代、网络与安全等场景实践。更多超融合金融核心生产业务场景实践&#xff0c;欢迎阅读文末电子书。 在金融行业如火如荼的数字化转型大潮中&#xff0c;传统架…

路由器如何映射端口映射?

在现代互联网中&#xff0c;随着网络应用的不断发展&#xff0c;很多用户需要进行远程访问或搭建服务器来满足自己的需求。由于网络安全的原因&#xff0c;直接将内网设备暴露在公网中是非常危险的。为了解决这个问题&#xff0c;路由器映射端口映射技术应运而生。本文将介绍什…