Notice
Recent Posts
Recent Comments
Link
.Zzumbong
[leetCode/JS] 1657. Determine if Two Strings Are Close ๋ณธ๋ฌธ
coding test/leetCode
[leetCode/JS] 1657. Determine if Two Strings Are Close
์ญ๋ด 2022. 12. 2. 11:26๋์ด๋ [ ๐ค ] Medium
๋ฌธ์ ์ค๋ช
Two strings are considered close if you can attain one from the other using the following operations:
- Operation 1: Swap any two existing characters.
- For example,
abcde -> aecdb
- For example,
- Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
- For example,
aacabb -> bbcbaa
(alla
's turn intob
's, and allb
's turn intoa
's)
- For example,
You can use the operations on either string as many times as necessary.
Given two strings, word1
and word2
, return true
if word1
and word2
are close, and false
otherwise.
๋ฌธ์๋ฅผ ์ด๋ํ๊ณ ๋ฐ๊พธ๊ณ ๋ฌธ์ ๊ฐ ๊ฒ๋ ๋ณต์กํ๋ฐ, ์์ ๋ฅผ ์ดํด๋ณด๋ฉด ์กฐ๊ฑด๋ค์ด ์์ด์ ์กฐ๊ธ ๋ ๋ณต์กํ๋ค.
1. ๋ฑ์ฅํ ๋ฌธ์์ ์ข ๋ฅ๊ฐ ๊ฐ์์ผํ๋ค.
cabbba// abbccc๋ ๊ฐ๊ฐ a,b,c ๋ง ๋ฑ์ฅํ๊ธฐ ๋๋ฌธ์ true์ด๋ค.
๋ง์ฝ aabbcc // aabbcs ๋ผ๋ฉด, abc // abcs ๋ก false์ด๋ค.
2. ๋ฌธ์์ ๋ฑ์ฅํ ์ซ์๊ฐ ์๋ก ๊ฐ์์ผ ํ๋ค.
cabbba // abbccc๋ ๊ฐ๊ฐ {a: 2, b: 3, c:1} // {a:1, b:2, c:3} ์ผ๋ก 231 // 123์ผ๋ก ์๋ก 1, 2, 3์ ๊ฐ์ง๊ณ ์๋ค. true.
๋ง์ฝ abbzzca // babzzcz๋ {a:2, b:2, c:1, z:1} // {a:1, b:2, c:1, z:3} // 2211, 1213 ์ผ๋ก false๊ฐ ๋๋ค.
์ ์ถ๋ ฅ ์
Example 1:
Input: word1 = "abc", word2 = "bca"
Output: true
Explanation: You can attain word2 from word1 in 2 operations.
Apply Operation 1: "abc" -> "acb"
Apply Operation 1: "acb" -> "bca"
Example 2:
Input: word1 = "a", word2 = "aa"
Output: false
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
Example 3
Input: word1 = "cabbba", word2 = "abbccc"
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -> "caabbb"
Apply Operation 2: "caabbb" -> "baaccc"
Apply Operation 2: "baaccc" -> "abbccc"
Constraints
1 <= word1.length, word2.length <= 105
word1
andword2
contain only lowercase English letters.
๋ด ์๋ฃจ์
- ๊ฐ word ๋ง๋ค hashmap ์ปจ์ ์ผ๋ก ๋ฌธ์์ข ๋ฅ, ๋ฌธ์๋ง๋ค ๋ฑ์ฅํ์๋ฅผ sort, join ํด์ ๋น๊ตํ๋ค.
/**
* @param {string} word1
* @param {string} word2
* @return {boolean}
*/
var closeStrings = function(word1, word2) {
// word1 = "cabbba" word2 = "abbccc"
if(word1.length !== word2.length) return false;
const get = (word) => {
const map = {};
for(let i = 0; i < word.length; i++) {
if(!map[word[i]]) map[word[i]] = 1;
else map[word[i]]++;
}
return [Object.keys(map).sort((a,b)=>a>b?-1:1).join(''), Object.values(map).sort((a,b)=>a-b).join('')];
}
const a = get(word1); // [ 'abc', '123' ]
const b = get(word2); // [ 'abc', '123' ]
return a[0] === b[0] && a[1] === b[1];
};
๊ฐ์ํ
- ๋ด๋ถํจ์๋ ๋ง๋ค๊ณ for, sort, join์ ์ฌ๋ฌ ๋ฒํด์ ์๊ฐ๋ณด๋ค ๋๋ฆด์ค ์์๋๋ฐ, ์ ๋นํ๋ฏ?
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 2256. Minimum Average Difference (0) | 2022.12.04 |
---|---|
[leetCode/JS] 451. Sort Characters By Frequency (0) | 2022.12.03 |
[leetCode/JS] 1704. Determine if String Halves Are Alike (0) | 2022.12.01 |
[leetCode/JS] 1207. Unique Number of Occurrences (0) | 2022.11.30 |
[leetCode/JS] 380. Insert Delete GetRandom O(1) (0) | 2022.11.29 |
0 Comments