​LeetCode刷題實戰161:相隔為1的編輯距離

2021-03-02 程序IT圈
算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 相隔為 1 的編輯距離  ,我們先來看題面:https://leetcode-cn.com/problems/one-edit-distance/

Given two strings S and T, determine if they are both one edit distance apart.

題意給定兩個字符串 s 和 t,判斷他們的編輯距離是否為 1。

示例 1:

輸入: s = "ab", t = "acb"
輸出: true
解釋: 可以將 'c' 插入字符串 s 來得到 t。

示例 2:

輸入: s = "cab", t = "ad"
輸出: false
解釋: 無法通過 1 步操作使 s 變為 t。

示例 3:

輸入: s = "1203", t = "1213"
輸出: true
解釋: 可以將字符串 s 中的 '0' 替換為 '1' 來得到 t。


解題https://blog.csdn.net/hebtu666/article/details/104107779但是稍微有一點優化,其實如果s能刪除一個字符變成t,那麼t一定也可以插入一個字符得到s。所以刪除插入沒必要都寫,我們把短的字符串放在左邊(s),就不用寫刪除了。

class Solution {
  public boolean isOneEditDistance(String s, String t) {
    int ns = s.length();
    int nt = t.length();
    //交換
    if (ns > nt)
      return isOneEditDistance(t, s);
    //長度差超過1
    if (nt - ns > 1)return false;
    //找到不同的字符
    for (int i = 0; i < ns; i++)
      if (s.charAt(i) != t.charAt(i))
        //如長度不同,那麼兩字符串後面應該相同
        if (ns == nt)
          return s.substring(i + 1).equals(t.substring(i + 1));
        //如長度不同,那麼必須在s[i]插入t[i],所以要保證s.substring(i).equals(t.substring(i + 1))
        else
          return s.substring(i).equals(t.substring(i + 1));
    return (ns + 1 == nt);
  }
}

好了,今天的文章就到這裡,如果覺得有所收穫,請順手點個在看或者轉發吧,你們的支持是我最大的動力 。

相關焦點

  • ​LeetCode刷題實戰72:編輯距離
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 編輯距離,我們先來看題面:https://leetcode-cn.com/problems/edit-distanceGiven two words word1 and word2, find the minimum number of operations required to convert word1
  • ​LeetCode刷題實戰139:單詞拆分
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !題意給定一個非空字符串 s 和一個包含非空單詞的列表 wordDict,判定 s 是否可以被空格拆分為一個或多個在字典中出現的單詞。
  • 帶你狂刷算法Leetcode題!短時間內快速獲得實戰能力!
    但對於初學者來說,很容易沉迷在刷題的數量中,覺得如果能刷完這1000道題,自己一定能夠有所飛躍。但實際上,低效率的重複對你來說,根本就無法掌握到解題的精髓,一旦題目有所變動,就無法舉一反三。那究竟應該怎麼刷題才高效呢?
  • ​LeetCode刷題實戰79:單詞搜索
    今天和大家聊的問題叫做 單詞搜索,我們先來看題面:https://leetcode-cn.com/problems/word-search/Given a 2D board and a word, find if the word exists in the grid.
  • ​LeetCode刷題實戰85: 最大矩形
    今天和大家聊的問題叫做 最大矩形,我們先來看題面:https://leetcode-cn.com/problems/maximal-rectangle/Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing
  • ​LeetCode刷題實戰198:打家劫舍
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 打家劫舍,我們先來看題面:https://leetcode-cn.com/problems/house-robber/You are a professional robber planning to rob houses along a street.
  • ​LeetCode刷題實戰42:接雨水
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 接雨水,我們先來看題面:https://leetcode-cn.com/problems/trapping-rain-water/Given n non-negative integers representing an elevation map where the width of each bar
  • ​LeetCode刷題實戰36: 有效的數獨
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 有效的數獨,我們先來看題面:https://leetcode-cn.com/problems/valid-sudoku/Determine if a 9x9 Sudoku board is valid.
  • ​LeetCode刷題實戰134:加油站
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !https://leetcode-cn.com/problems/gas-station/There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
  • ​LeetCode刷題實戰179:最大數
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 最大數  ,我們先來看題面:https://leetcode-cn.com/problems/largest-number/Given a list of non-negative integers nums, arrange them such that they form the largest number.
  • ​LeetCode刷題實戰57:插入區間
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 插入區間,我們先來看題面:https://leetcode-cn.com/problems/insert-interval/Given a set of non-overlapping intervals, insert a new interval into the intervals (merge
  • ​LeetCode刷題實戰133:克隆圖
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !https://leetcode-cn.com/problems/clone-graph/Given a reference of a node in a connected undirected graph.
  • ​LeetCode刷題實戰70:爬樓梯
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 爬樓梯,我們先來看題面:https://leetcode-cn.com/problems/climbing-stairs/You are climbing a stair case.
  • ​LeetCode刷題實戰54:螺旋矩陣
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 螺旋矩陣,我們先來看題面:https://leetcode-cn.com/problems/spiral-matrix/Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in
  • ​LeetCode刷題實戰39:組合總和
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 組合總和,我們先來看題面:https://leetcode-cn.com/problems/combination-sumGiven a set of candidate numbers (candidates) (without duplicates) and a target number (target
  • leetcode刷題指南之PatchingArray
    問: 最少需要往數組中添加多少個數才能使得1 ~ n的數都能由數組中若干數的和得到?掃描數組, 令sum表示當前我們能得到1 ~ sum的值。那麼接下來的數x如果小於等於sum+1, 那麼我們直接加進來更新sum的值(sum += x), 否則, 我們為了得到sum+1這個數, 我們必然要加入一個數。顯然加入sum+1這個數是最優決策。
  • 春節大禮包|刷題技巧+80道Leetcode
    為了跳槽,我前兩年的春節都是在刷題中度過的,目前為止刷了小四百道leetcode,也算是有一些經驗,今天就跟大家分享下學習方法和我總結的乾貨。後來發現了 Leetbook[1] 這個寶藏,才算是找到了適合自己的刷題方法。
  • ​LeetCode刷題實戰119: 楊輝三角 II
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 楊輝三角 II,我們先來看題面:https://leetcode-cn.com/problems/pascals-triangle-ii/Given an integer rowIndex, return the rowIndexth row of the Pascal's triangle.
  • ​LeetCode刷題實戰18: 四數之和
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做四數之和 ,我們先來看題面:https://leetcode-cn.com/problems/4sum/Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such
  • LeetCode刷題第三周【數組(簡單)】
    參考資料[1]Leetcode網站: https://leetcode-cn.com/[2]以上文字描述均來自 LeetCode數組模塊: https://leetcode-cn.com/tag/array/[3]刷題請點擊或見附錄:1539.