当前位置:网站首页>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).
边栏推荐
- What is the pledge pool and how to pledge?
- Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
- 數據分析必備的能力
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- Idea configuration plug-in
- Cocos Creator 2. X automatic packaging (build + compile)
- Web crawler knowledge day03
- Leetcode binary search tree
- What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
- 什么是质押池,如何进行质押呢?
猜你喜欢

QT串口ui设计和解决显示中文乱码

CC2530 common registers for port interrupts

NSQ源码安装运行过程

What is the maximum number of concurrent TCP connections for a server? 65535?

MySQL converts comma separated attribute field data from column to row

One article takes you to understand machine learning

ThreeJS 第二篇:顶点概念、几何体结构

关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM

CC2530 common registers for ADC single channel conversion
![[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](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[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
随机推荐
PHP secondary domain name session sharing scheme
MySQL single table field duplicate data takes the latest SQL statement
[combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
One article takes you to understand machine learning
CC2530 common registers for ADC single channel conversion
面试之 top k问题
Mysql database DDL and DML
深入理解 SQL 中的 Grouping Sets 语句
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
CC2530 common registers for watchdog
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
Shentong express expects an annual loss of nearly 1billion
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
數據分析必備的能力
(Supplement) double pointer topic
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
【剑指 Offer 】57 - II. 和为s的连续正数序列
Détails du contrôle de la congestion TCP | 3. Espace de conception