当前位置:网站首页>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**( allaTurn intob, And allbConvert 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 <= 105word1andword2Contains 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).
边栏推荐
- The word backspace key cannot delete the selected text, so you can only press Delete
- PHP中register_globals参数设置
- MySQL single table field duplicate data takes the latest SQL statement
- Basis of target detection (IOU)
- IDEA-配置插件
- arduino-esp32:LVGL项目(一)整体框架
- To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
- NSQ源码安装运行过程
- 8 tips for effective performance evaluation
- Explore Netease's large-scale automated testing solutions see here see here
猜你喜欢

美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃

NSQ源码安装运行过程

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties

CC2530 common registers for port initialization

QT serial port UI design and solution to display Chinese garbled code

Add color to the interface automation test framework and realize the enterprise wechat test report
![[solved] access denied for user 'root' @ 'localhost' (using password: yes)](/img/71/1ff8ed1d773da99054310f96dca3f8.jpg)
[solved] access denied for user 'root' @ 'localhost' (using password: yes)

arduino-esp32:LVGL项目(一)整体框架

Thread pool executes scheduled tasks
随机推荐
Cocos Creator 2.x 自动打包(构建 + 编译)
What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
Nifi from introduction to practice (nanny level tutorial) - flow
Overview of satellite navigation system
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
CC2530 common registers
(补)双指针专题
Détails du contrôle de la congestion TCP | 3. Espace de conception
Cocos Creator 2. X automatic packaging (build + compile)
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
IL Runtime
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
关于学习Qt编程的好书精品推荐
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
斑马识别成狗,AI犯错的原因被斯坦福找到了
Thread pool executes scheduled tasks