当前位置:网站首页>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).
边栏推荐
- Leetcode binary search tree
- Characteristic polynomial and constant coefficient homogeneous linear recurrence
- [combinatorics] non descending path problem (number of non descending paths with constraints)
- 程序猿如何快速成长
- Explore Netease's large-scale automated testing solutions see here see here
- Interviewer: how does the JVM allocate and recycle off heap memory
- NSQ source code installation and operation process
- How to set up SVN server on this machine
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- What material is sa537cl2? Analysis of mechanical properties of American standard container plate
猜你喜欢
Shentong express expects an annual loss of nearly 1billion
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
Explore Cassandra's decentralized distributed architecture
NSQ源码安装运行过程
Simulink oscilloscope data is imported into Matlab and drawn
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
Unreal_ Datatable implements ID self increment and sets rowname
Threejs Part 2: vertex concept, geometry structure
随机推荐
什么是质押池,如何进行质押呢?
Mysql 单表字段重复数据取最新一条sql语句
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
Client does not support authentication protocol requested by server; consider upgrading MySQL client
PHP converts a one-dimensional array into a two-dimensional array
Daily code 300 lines learning notes day 10
Acwing game 58
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
MySQL single table field duplicate data takes the latest SQL statement
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
CC2530 common registers for serial communication
Shentong express expects an annual loss of nearly 1billion
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Golang anonymous function use
Visual SLAM algorithms: a survey from 2010 to 2016
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Top k questions of interview
爱可可AI前沿推介(7.3)
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day