当前位置: 首页 > news >正文

leetcode - 2825. Make String a Subsequence Using Cyclic Increments

Description

You are given two 0-indexed strings str1 and str2.

In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is ‘a’ becomes ‘b’, ‘b’ becomes ‘c’, and so on, and ‘z’ becomes ‘a’.

Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise.

Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.

Example 1:

Input: str1 = "abc", str2 = "ad"
Output: true
Explanation: Select index 2 in str1.
Increment str1[2] to become 'd'. 
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.

Example 2:

Input: str1 = "zc", str2 = "ad"
Output: true
Explanation: Select indices 0 and 1 in str1. 
Increment str1[0] to become 'a'. 
Increment str1[1] to become 'd'. 
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.

Example 3:

Input: str1 = "ab", str2 = "d"
Output: false
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. 
Therefore, false is returned.

Constraints:

1 <= str1.length <= 10^5
1 <= str2.length <= 10^5
str1 and str2 consist of only lowercase English letters.

Solution

Solved after hints…

It’s like how we decide if str2 is a subsequence of str1, only this time we could change str1. To decide the subsequence, we could use 2 pointers on each string. If the str1[i] is the same as str2[j], move both i, j forward. Otherwise if str1[i] can increment cyclically and they match, we also move i, j both forward. Otherwise we only move i forward.

After comparing, if j reaches at the end of str2, then it’s a subsequence of str1.

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

Code

class Solution:def canMakeSubsequence(self, str1: str, str2: str) -> bool:p1, p2 = 0, 0while p1 < len(str1) and p2 < len(str2):if str1[p1] == str2[p2]:p1 += 1p2 += 1elif chr((ord(str1[p1]) + 1 - ord('a')) % 26 + ord('a')) == str2[p2]:p1 += 1p2 += 1else:p1 += 1return p2 == len(str2)
http://www.lryc.cn/news/497579.html

相关文章:

  • 工业—使用Flink处理Kafka中的数据_ChangeRecord1
  • 探索嵌入式硬件设计:揭秘智能设备的心脏
  • 数据结构-最小生成树
  • mac启动jmeter
  • spring学习笔记之静态代理和动态代理
  • qemu搭建aarch64
  • delphi IDE 插件DelphiIDEPlugin_SearchProject,用于从项目组中查找项目
  • 【Vue】Scoped、组件间通信、Props检验
  • openbmc dbus架构简析(二)
  • 【二分查找】Leetcode例题
  • gitlab配置调试minio
  • Vue实战技巧:如何展示附件(PDF、MP4、Excel、Zip等)并修改名称下载
  • AI证件照制作 API 对接说明
  • Macos用brew安装Nodejs亲手教程
  • Node.js 新手教程
  • Latex转word(docx)或者说PDF转word 一个相对靠谱的方式
  • 前端热门面试题目——React、Node
  • Ansible自动化一键部署单节点集群架构
  • 电脑插入耳机和音响,只显示一个播放设备
  • 家政小程序开发,打造便捷家政生活小程序
  • tcpdump抓包wireshark分析
  • 文件无法直接拖入zotero
  • 使用 useMemo 和 React.memo 优化 React 组件渲染
  • ISAAC SIM踩坑记录--添加第三方3D场景
  • Git 详解
  • Linux操作系统3-文件与IO操作1(从C语言IO操作到系统调用)
  • 【Python网络爬虫笔记】8- (BeautifulSoup)抓取电影天堂2024年最新电影,并保存所有电影名称和链接
  • Rancher V2.7.0安装教程
  • STM32MX 配置CANFD收发通讯
  • (12)时间序列预测之MICN(CNN)