当前位置:网站首页>LeetCode 1657. Determine whether the two strings are close
LeetCode 1657. Determine whether the two strings are close
2022-07-03 16:44:00 【Daylight629】
1657. Determine whether two strings are close to
If you can use the following operations to get a string from one string to another , Two strings near :
operation 1: Exchange any two
existing
character .
- for example ,
a**b**cd**e** -> a**e**cd**b**
- for example ,
operation 2: Will a
existing
Each occurrence of a character is converted to another
existing
character , And do the same thing for another character .
- for example ,
**aa**c**abb** -> **bb**c**baa**
( alla
Turn intob
, And allb
Convert toa
)
- for example ,
You can use these two operations multiple times for any string you want .
Here are two strings ,word1
and word2
. If word1
and word2
near , Just go back to true
; otherwise , return false
.
Example 1:
Input :word1 = "abc", word2 = "bca"
Output :true
explain :2 Operations from word1 get word2 .
Perform the operation 1:"abc" -> "acb"
Perform the operation 1:"acb" -> "bca"
Example 2:
Input :word1 = "a", word2 = "aa"
Output :false
explain : No matter how many operations are performed , I can't get it from word1 obtain word2 , vice versa .
Example 3:
Input :word1 = "cabbba", word2 = "abbccc"
Output :true
explain :3 Operations from word1 get word2 .
Perform the operation 1:"cabbba" -> "caabbb"
Perform the operation 2:"caabbb" -> "baaccc"
Perform the operation 2:"baaccc" -> "abbccc"
Example 4:
Input :word1 = "cabbba", word2 = "aabbss"
Output :false
explain : No matter how many operations are performed , I can't get it from word1 obtain word2 , vice versa .
Tips :
1 <= word1.length, word2.length <= 105
word1
andword2
Contains only lowercase English letters
Two 、 Method 1
automata
class Solution {
public boolean closeStrings(String word1, String word2) {
char[] arr = word1.toCharArray();
char[] brr= word2.toCharArray();
if (arr.length == brr.length) {
int[] mark1 = new int[26];
int[] mark2 = new int[26];
for (char c : arr) {
mark1[c - 'a']++;
}
for (char c : brr) {
if (mark1[c - 'a'] == 0) {
return false;
}
mark2[c - 'a']++;
}
Arrays.sort(mark1);
Arrays.sort(mark2);
for (int i = 0; i < mark1.length; i++) {
if (mark1[i] != mark2[i]) {
return false;
}
}
return true;
}
return false;
}
}
Complexity analysis
Time complexity :O(nlogn)..
Spatial complexity :O(n).
边栏推荐
- 香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- [Jianzhi offer] 58 - ii Rotate string left
- PHP converts a one-dimensional array into a two-dimensional array
- 关于学习Qt编程的好书精品推荐
- Nifi from introduction to practice (nanny level tutorial) - flow
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- PHP CI (CodeIgniter) log level setting
- What is the maximum number of concurrent TCP connections for a server? 65535?
猜你喜欢
Daily code 300 lines learning notes day 10
[statement] about searching sogk1997 and finding many web crawler results
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
A survey of state of the art on visual slam
CC2530 common registers for watchdog
NSQ源码安装运行过程
Learn from me about the enterprise flutter project: simplified framework demo reference
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
线程池执行定时任务
随机推荐
CC2530 common registers for timer 1
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
Détails du contrôle de la congestion TCP | 3. Espace de conception
消息队列消息丢失和消息重复发送的处理策略
如何在本机搭建SVN服务器
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Client does not support authentication protocol requested by server; consider upgrading MySQL client
NSQ源码安装运行过程
斑马识别成狗,AI犯错的原因被斯坦福找到了
Data driving of appium framework for mobile terminal automated testing
爱可可AI前沿推介(7.3)
[combinatorial mathematics] counting model, common combinatorial numbers and combinatorial identities**
Basis of target detection (IOU)
PHP CI(CodeIgniter)log级别设置
[combinatorics] non descending path problem (number of non descending paths with constraints)
Golang 匿名函数使用
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
Caching mechanism of Hibernate / session level caching mechanism
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征