日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode1153. 字符串转化 | String Transforms Into Another String

發(fā)布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode1153. 字符串转化 | String Transforms Into Another String 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:為敢(WeiGanTechnologies)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11333863.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given two strings?str1?and?str2?of the same length, determine whether you can transform?str1?into?str2?by doing?zero or more?conversions.

In one conversion you can convert?all?occurrences of one character in?str1?to?any?other lowercase English character.

Return?true?if and only if you can transform?str1?into?str2.

Example 1:

Input: str1 = "aabcc", str2 = "ccdee" Output: true Explanation: Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'. Note that the order of conversions matter.

Example 2:

Input: str1 = "leetcode", str2 = "codeleet" Output: false Explanation: There is no way to transform str1 to str2.

Note:

  • 1 <= str1.length == str2.length <= 10^4
  • Both?str1?and?str2?contain only lowercase English letters.

  • 給出兩個長度相同的字符串,分別是?str1?和?str2。請你幫忙判斷字符串?str1?能不能在?零次?或?多次?轉化后變成字符串?str2。

    每一次轉化時,將會一次性將?str1?中出現(xiàn)的?所有?相同字母變成其他?任何?小寫英文字母(見示例)。

    只有在字符串?str1?能夠通過上述方式順利轉化為字符串?str2?時才能返回?True,否則返回?False。??

    示例 1:

    輸入:str1 = "aabcc", str2 = "ccdee" 輸出:true 解釋:將 'c' 變成 'e',然后把 'b' 變成 'd',接著再把 'a' 變成 'c'。注意,轉化的順序也很重要。

    示例 2:

    輸入:str1 = "leetcode", str2 = "codeleet" 輸出:false 解釋:我們沒有辦法能夠把 str1 轉化為 str2。

    提示:

  • 1 <= str1.length == str2.length <= 10^4
  • str1?和?str2?中都只會出現(xiàn)?小寫英文字母

  • 72ms

    1 class Solution { 2 func canConvert(_ str1: String, _ str2: String) -> Bool { 3 if str1 == str2 {return true} 4 let n:Int = str1.count 5 var arr:[Int] = [Int](repeating:-1,count:26) 6 let arrS:[Int] = Array(str1).map{$0.ascii} 7 let arrT:[Int] = Array(str2).map{$0.ascii} 8 for i in 0..<n 9 { 10 var x:Int = arrS[i] - 97 11 var y:Int = arrT[i] - 97 12 if arr[x] == -1 13 { 14 arr[x] = y 15 } 16 else if arr[x] != y 17 { 18 return false 19 } 20 } 21 var has:Int = 0 22 for i in 0..<26 23 { 24 if arr[i] != -1 25 { 26 has += 1 27 } 28 } 29 var flag:Int = 0 30 for i in 0..<26 31 { 32 for j in (i + 1)..<26 33 { 34 if arr[i] != -1 && arr[j] != -1 && arr[i] == arr[j] 35 { 36 flag = 1 37 } 38 } 39 } 40 if has != 26 || flag != 0 {return true} 41 return false 42 } 43 } 44 45 //Character擴展 46 extension Character 47 { 48 //Character轉ASCII整數(shù)值(定義小寫為整數(shù)值) 49 var ascii: Int { 50 get { 51 return Int(self.unicodeScalars.first?.value ?? 0) 52 } 53 } 54 }

    ?

    轉載于:https://www.cnblogs.com/strengthen/p/11333863.html

    總結

    以上是生活随笔為你收集整理的[Swift]LeetCode1153. 字符串转化 | String Transforms Into Another String的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。