LeetCode 161. One Edit Distance

『杭电1199』Color the Ball Problem Description There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to 阅读详情
#include <string>
#include <iostream>
using namespace std;

/*
  Given two strings S and T, determine if they are both one edit distance apart.
*/
bool isOneEditDistance(string s, string t) {
  int sSize = s.size();
  int tSize = t.size();
  if(abs(sSize - tSize) > 1) return false;
  int count = 0;

  if(sSize == tSize) {
    int i = 0, j = 0;
    while(i < sSize && j < tSize) {
      if(s[i] != t[i]) {
        count++;
        if(count > 1) return false;
      }
      i++; j++;
    }
    return true;
  } else {
    string tmp = sSize > tSize ? s : t;
    string tmp_1 = sSize > tSize ? t : s;
    int i = 0, j = 0;
    while(i < sSize && j < tSize) {
      if((tmp[i] != tmp_1[j]) && (count != 0)) {
        return false;
      } else if(tmp[i] != tmp_1[j]) {
        count++;
        i++;
      } else {
        i++;
        j++;
      }
    }
    return true;
  }
}

int main(void) {
  string a = "abc";
  string b = "ab";
  cout << isOneEditDistance(a, b) << endl;
}
~                 


10倍开发者的 Dify 魔法书:从零构建全栈 AI 应用 “不懂底层算法,也能开发爆款 AI 应用吗?” 答案是:绝对可以。 Dify 正在重新定义 AI 应用的开发范式——通过可视化的低代码平台,将原本需要数周的开发周期压缩到几小时。无论你是前端、后端、产品经理,还是渴望破局的独立开发者,这都是你不可错过的红利。 我们将手把手教你利用 Dify 快速接入百模大战,零代码搭建企业级知识库,并一键发布你的专属 AI 助手、智能客服和自动化工作流。 告别重复造轮子,掌握 Dify,一个人就是一支 AI 团队。现在上车,开启你的 10 倍速开发之旅! 阅读详情

相关推荐

智能座舱-语音交互系统

从什么是智能座舱、智能座舱的发展驱动因素、智能座舱的构成要素三个方面梳理我对智能座舱的基础认识之后,为了加深“智能座舱产品入门”课程中语音交互部分知识的理解,我从什么是语音交互、语音交互的底层技术、智能座舱的语音交互等方面,对智能座舱语音交互系统相关的知识进行了梳理与总结。

GUOSA0928的博客 1458

LeetCode: 第 161 场周赛题解

LeetCode: 第 161 场周赛题解 5247. 交换字符使得字符串相同 题目 有两个长度相同的字符串 s1 和 s2,且它们其中 只含有 字符 "x" 和 "y",你需要通过「交换字符」的方式使这两个字符串相同。 每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。 交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 s1[i] 和 ...

风笛叶的博客 455

Ubuntu开发环境配置——持续更新

本文简要介绍了Ubuntu下的开发环境配置流程和问题分析与解决,更多内容持续更新中。

m0_53365889的博客 2228

【LeetCode - 161】相隔为 1 的编辑距离

【中等】字符串、动态规划

学哥斌的博客 1170

告别MATLAB依赖:手把手教你用Python实现GCC-PHAT时延估计(附完整代码与对比测试)

本文详细介绍了如何使用Python实现GCC-PHAT时延估计算法,替代传统的MATLAB依赖。通过完整的代码示例和性能对比测试,展示了Python在信号处理领域的强大能力,特别是在广义互相关和TDOA应用中的高效实现。文章还提供了实时音频处理和嵌入式部署的实用技巧,帮助工程师快速掌握这一关键技术。

weixin_42667269的博客 223

2017阿里内推笔试题--算法工程师(运筹优化)

2017阿里内推笔试题–算法工程师(运筹优化)题目沐哲是一个菜鸟仓库的一个拣货员,但他有非常个怪异的习惯。每次拣货的重量都要比之前拣的一个轻,每次拣到货后都可以得到1块钱,沐哲想知道这样最多能赚多少钱 32 34 7 33 21 2 13 12 3 11 26 36 16 30 22 1 24 14 20 23 25 5 19 29 27 15 9 17 31 4 6 18 8 10 3

shawpan的专栏 1万+

LeetCode第161题_相隔为1的编辑距离

给定两个字符串s和t,判断他们的编辑距离是否为 1。ststst。

qq_40263592的博客 865

anaconda navigator 卡在loading application界面一直没有反应解决办法

在打开anaconda navigator 程序的时候,遇到了2个问题。第一个问题:打开的时候弹出提示:There is an instance of Anaconda Navigator already running.第二个问题:程序一直卡在loading application界面一直没有反应。以下是我的解决办法,有效果。

猿小白的博客 4394

力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)

给定两个字符串 s 和 t ,如果它们的编辑距离为 1 ,则返回 true ,否则返回 false 。字符串 s 和字符串 t 之间满足编辑距离等于 1 有三种可能的情形:往 s 中插入 恰好一个 字符得到 t 从 s 中删除 恰好一个 字符得到 t 在 s 中用 一个不同的字符 替换 恰好一个 字符得到 t示例 1:示例 2:提示:来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/one-edit-distance方法一:双指针Python3提交内容:...

ChaoYue_miku的博客 695

流媒体之LL-HLS 低延时HLS协议

LL-HLS协议通过扩展了HLS协议,为实时直播场景提供了更丰富的功能,如延迟控制、播放列表增量更新、阻止播放列表重新加载等。同时客户端可以更灵活地控制播放列表的加载和播放,以适应各种场景和需求。LL-HLS在直播中的延时可以控制在3s内, 但是即使这样, 还是不如RTMP, RTSP等协议的实时性。

技术控的笔记 3371

ardupilot开发 --- mavlink签名加密 篇

ardupilot开发 --- mavlink签名加密

记录老板的每次pua,分享领导每天的毒鸡汤... 1249

LeetCode刷题笔记(Java)---第161-180题

文章目录162. 寻找峰值164. 最大间距165. 比较版本号 162. 寻找峰值 峰值元素是指其值大于左右相邻值的元素。 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。 你可以假设 nums[-1] = nums[n] = -∞。 解答 //二分查找 public int findPeakElement(int[] nums) { int lef

gongsenlin341的博客 768

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode 161. One Edit Distance--Python,Java,C++解法

LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目Java和Python的解法都有。 欢迎大家关注我的专栏。 此题链接:One Edit Distance - LeetCode 这道题目进阶版本Edit Distance - LeetCode 文章地址:LeetCode 72. Edit Distance - zhangpeterx的...

个人博客 1055

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[LeetCode]161. One Edit Distance

原题链接:https://leetcode.com/problems/one-edit-distance/ 这道题目及变种多次在各大公司的面经中出现过。我在面snapchat的时候也遇到过这道题目。题目是说我们要求给定2个输入字符串S和T,要求判断S和T是不是只有一个字符只差。这里One Edit Distance意思就是你改动其中一个字符串的1个字符,就会变成另一个字符串。这里的改动包括增加1

foolman 656

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【LeetCode】161. One Edit Distance

LeetCode161 传送门 解法: 推荐解法和我的解法看似类似,时间、空间复杂度非常接近,但实则简洁很多。究其原因,推荐方法中采用了分段的思想。这种分段的思想很适合于字符串问题。 Python源码: Runtime:24 ms, faster than28.87%ofPythononline submissions forOne Edit Distance. Me...

weixin_42344158的博客 282

leetcode 161: One Edit Distance

One Edit Distance Total Accepted: 709 Total Submissions: 3097 Given two strings S and T, determine if they are both one edit distance apart. [分析] delete, add, replace 三种都是 One edit dista

西施豆腐渣 5900

Leetcode 161 One Edit Distance

思路:分为三个部分来做,delete,replace,insert。 delete很容易,只需要把长度大的那个string遍历remove每个元素,每remove一个元素,就和另一个string比较是否相等,如果想等就返回true。 replace也很容易,此时两个string必定是相等长度的,因为本题我们只修改s,以符合t,所以我们只需要将t中的每一个元素挨个替换s中的每一个元素,每替换一次,就进行一次比较,如相等,返回true。 insert,此时肯定是s比t长度小1,insert一个字母后两者相等。.

GoodJobJasper的博客 322

leetcode[161] One Edit Distance

判断两个字符串的编辑距离是不是1. 关于编辑距离可以参见之前有一题leetcode[72] Edit Distance 思路: 如果字符串相差2个以及以上长度,那么肯定不止1,直接false 如果字符串长度相等,那么判断对应位置不同的字符数是不是1即可。 如果字符串长度相差1,那么肯定是要在长的那个串删掉一个,所以两个字符串一起加加,一旦遇到一个不同,那么剩下的子串就要是一样,...

175

LeetCode 161 One Edit Distance

Problem: Given two strings S and T, determine if they are both one edit distance apart. Analyse: delete, add, replace 都是one edit distance. delete 和 add: 两个string 的长度相差一 replace: 两个string的长度相同 伪代码: str...

Opium_Z的博客 199

【Leetcode】161. One Edit Distance

题目地址: https://leetcode.com/problems/one-edit-distance/ 给定两个字符串sss和ttt,问sss可否由下列三种变换变为ttt: 1、在sss中插入一个字符; 2、在sss中删除一个字符; 3、在sss中替换一个字符。 如果可以就返回true,否则返回false。 首先排除掉sss与ttt长度差大于等于222或者sss和ttt相等的情况,这两种情况...

数学、算法爱好者的博客 267

LeetCode String 161 One Edit Distance

161. One Edit Distance Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Insert a character into s to g...

Yi Jiao's Blog 171

LeetCode 161. One Edit Distance (Java版; Medium)

welcome to my blog LeetCode 161. One Edit Distance (Java版; Medium) 题目描述 Given two strings s and t, determine if they are both one edit distance apart. Note:  There are 3 possiblities to sa...

littlehaes的博客 283

LeetCode—161. One Edit Distance

Binary Tree Upside Down思路:其实分类讨论更容易一次过,考虑长度相等和差1的情况,特别注意,s.equals(t)是false GitHub地址:https://github.com/corpsepiges/leetcode 点此进入如果可以的话,请点一下star,谢谢。 public class Solution {

徐玄清的专栏 694

[LeetCode161] One Edit Distance

Given two strings S and T, determine if they are both one edit distance apart.Hide Company Tags Uber Facebook Hide Tags String Hide Similar Problems (H) Edit Distance 所谓Edit distance, 一次delete, insert

MoM 352

[leetcode] 161. One Edit Distance @ python

原题 Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Insert a character into s to get t Delete a charac...

闲庭信步 466

[Leetcode] 161. One Edit Distance 解题报告

题目: Given two strings S and T, determine if they are both one edit distance apart. 思路: One Edit Distance意味着一个字符串s可以通过删除,增加或者修改其中的一个字符变成另外一个字符串t。因此我们可以根据s和t的长度分别处理:如果两者长度相同,则判断s和t是否仅仅有一个字符不相同;如果t的

魔豆(Magicbean)的博客 814
上一篇: LeetCode 295. Find Median from Data Stream
下一篇: LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值