当前位置:网站首页>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).
边栏推荐
- Top k questions of interview
- Golang decorator mode and its use in NSQ
- CC2530 common registers
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- Visual SLAM algorithms: a survey from 2010 to 2016
- 【剑指 Offer】58 - I. 翻转单词顺序
- NSQ源码安装运行过程
- Summary of three methods of PHP looping through arrays list (), each (), and while
- 消息队列消息丢失和消息重复发送的处理策略
- [combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
猜你喜欢

Deep understanding of grouping sets statements in SQL

Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford

【LeetCode】94. Middle order traversal of binary tree

Explore Cassandra's decentralized distributed architecture

(Supplement) double pointer topic

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

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

Aike AI frontier promotion (7.3)

CC2530 common registers for port interrupts
随机推荐
浅谈拉格朗日插值及其应用
线程池执行定时任务
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Learn from me about the enterprise flutter project: simplified framework demo reference
Threejs Part 2: vertex concept, geometry structure
14 topics for performance interviews between superiors and subordinates (4)
function overloading
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Pointcut expression
A survey of state of the art on visual slam
[Jianzhi offer] 58 - ii Rotate string left
Extraction of the same pointcut
Interpretation of several important concepts of satellite antenna
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
CC2530 common registers for timer 1
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
2022爱分析· 国央企数字化厂商全景报告
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
Processing strategy of message queue message loss and repeated message sending