[LeetCode] 1657. Determine if Two Strings Are Close
Two strings are considered close if you can attain one from the other using the following operations:
- Operation 1: Swap any two existing characters.
- For example,
abcde -> aecdb
- For example,
- Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
- For example,
aacabb -> bbcbaa
(alla
's turn intob
's, and allb
's turn intoa
's)
- For example,
You can use the operations on either string as many times as necessary.
Given two strings, word1
and word2
, return true
if word1
and word2
are close, and false
otherwise.
Example 1:
Input: word1 = "abc", word2 = "bca" Output: true Explanation: You can attain word2 from word1 in 2 operations. Apply Operation 1: "abc" -> "acb" Apply Operation 1: "acb" -> "bca"
Example 2:
Input: word1 = "a", word2 = "aa" Output: false Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
Example 3:
Input: word1 = "cabbba", word2 = "abbccc"
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -> "caabbb"
Apply Operation 2: "
caabbb" -> "baaccc"
Apply Operation 2: "baaccc" -> "abbccc"
Example 4:
Input: word1 = "cabbba", word2 = "aabbss" Output: false Explanation: It is impossible to attain word2 from word1, or vice versa, in any amount of operations.
Constraints:
1 <= word1.length, word2.length <= 105
word1
andword2
contain only lowercase English letters.
确定两个字符串是否接近。
如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 接近 :
操作 1:交换任意两个 现有 字符。
例如,abcde -> aecdb
操作 2:将一个 现有 字符的每次出现转换为另一个 现有 字符,并对另一个字符执行相同的操作。
例如,aacabb -> bbcbaa(所有 a 转化为 b ,而所有的 b 转换为 a )
你可以根据需要对任意一个字符串多次使用这两种操作。给你两个字符串,word1 和 word2 。如果 word1 和 word2 接近 ,就返回 true ;否则,返回 false 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/determine-if-two-strings-are-close
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是贪心。这道题按照给的两种操作来判断,注意标红的条件,这两种操作可以被执行多次。
先说操作 1,如果要满足交换任意两个字符 char,使得 A 变成 B,那么这两个被交换的字符的出现次数一定要相同才行,也就是说在字符串 A 中如果你要替换某个字母 x,那么在字符串 B 中相同 index 位置上的那个字母 y 的出现次数要跟 x 相同才行。
对于操作 2,如果要满足将一个现有字符替换成另一个现有字符,比如题目给的例子,那么需要满足第一个字符串中 a 的数量 = 第二个字符串中 b 的数量,同时第一个字符串中 b 的数量 = 第二个字符串中 a 的数量。这两个条件转化成代码就是检查两个字符串中所有出现过的字母的数量是不是能一一对应(字母不一定相同)。参见下面这个例子,如果所有出现的字母的次数在排序后能对的上,则执行操作 2 就能满足题意。
String 1 = "aabaacczp" String 2="bbzbbaacp" Frequency in string1 : Frequency in string2 : a->4 b->4 b->1 a->2 c->2 z->1 z->1 c->1 p->1 p->1 see in String 1 count array -> 1, 1, 1, 2, 4 =>sorted order and in String 2 count array -> 1, 1, 1, 2, 4 =>sorted order Unique all char a,b,c,z,p in string 1 is there as well in string2 so it's a valid One just return True
时间O(nlogn)
空间O(n)
Java实现
1 class Solution { 2 public boolean closeStrings(String word1, String word2) { 3 // corner case 4 if (word1.length() != word2.length()) { 5 return false; 6 } 7 8 // normal case 9 int len = word1.length(); 10 int[] count1 = new int[26]; 11 int[] count2 = new int[26]; 12 for (int i = 0; i < len; i++) { 13 count1[word1.charAt(i) - 'a']++; 14 count2[word2.charAt(i) - 'a']++; 15 } 16 17 for (int i = 0; i < 26; i++) { 18 if (count1[i] > 0 && count2[i] == 0) { 19 return false; 20 } 21 if (count2[i] > 0 && count1[i] == 0) { 22 return false; 23 } 24 } 25 Arrays.sort(count1); 26 Arrays.sort(count2); 27 for (int i = 0; i < 26; i++) { 28 if (count1[i] != count2[i]) { 29 return false; 30 } 31 } 32 return true; 33 } 34 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~