You are given two 0-indexed strings str1 and str2.
In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'.
Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise.
Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.
Example 1:
Input: str1 = "abc", str2 = "ad"
Output: true
Explanation: Select index 2 in str1.
Increment str1[2] to become 'd'.
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.
Example 2:
Input: str1 = "zc", str2 = "ad"
Output: true
Explanation: Select indices 0 and 1 in str1.
Increment str1[0] to become 'a'.
Increment str1[1] to become 'd'.
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.
Example 3:
Input: str1 = "ab", str2 = "d"
Output: false
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once.
Therefore, false is returned.
출처 : https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/description/
풀이
function canMakeSubsequence(str1: string, str2: string): boolean {
let current = 0
for (let i = 0; i < str1.length; i++) {
if (str2[current] === undefined) {
break
}
const difference = str2[current].charCodeAt(0) - str1[i].charCodeAt(0)
if (difference === 1 || difference === 0 || difference === -25) {
current++
}
}
return current === str2.length
};
두 개의 문자열 str1과 str2가 주어졌을 때, str1에서 몇몇 문자를 선택해 str2를 만들 수 있는지 확인하는 함수
.charCodeAt(0)
- charCodeAt(0) 메서드는 문자열에서 특정 위치에 있는 문자의 유니코드 값을 반환
let str = "hello";
console.log(str.charCodeAt(0)); // 104 ('h'의 유니코드 값)
console.log(str.charCodeAt(1)); // 101 ('e'의 유니코드 값)
console.log(str.charCodeAt(2)); // 108 ('l'의 유니코드 값)
console.log(str.charCodeAt(3)); // 108 ('l'의 유니코드 값)
console.log(str.charCodeAt(4)); // 111 ('o'의 유니코드 값)
예시 1
- str1: "abc"
- str2: "bd"
- current는 0으로 시작.
- str1의 첫 번째 문자 'a'와 str2의 첫 번째 문자 'b'를 비교.
- 차이: 'b'의 ASCII 코드 (98) - 'a'의 ASCII 코드 (97) = 1
- 차이가 1이므로, current를 1로 증가.
- str1의 두 번째 문자 'b'와 str2의 두 번째 문자 'd'를 비교.
- 차이: 'd'의 ASCII 코드 (100) - 'b'의 ASCII 코드 (98) = 2
- 차이가 1이나 0, -25가 아니므로, current를 증가시키지 않음.
- str1의 세 번째 문자 'c'와 str2의 두 번째 문자 'd'를 비교.
- 차이: 'd'의 ASCII 코드 (100) - 'c'의 ASCII 코드 (99) = 1
- 차이가 1이므로, current를 2로 증가.
- current가 str2의 길이 2와 같으므로, true를 반환.
예시 2
- str1: "abz"
- str2: "ba"
- current는 0으로 시작.
- str1의 첫 번째 문자 'a'와 str2의 첫 번째 문자 'b'를 비교.
- 차이: 'b'의 ASCII 코드 (98) - 'a'의 ASCII 코드 (97) = 1
- 차이가 1이므로, current를 1로 증가.
- str1의 두 번째 문자 'b'와 str2의 두 번째 문자 'a'를 비교.
- 차이: 'a'의 ASCII 코드 (97) - 'b'의 ASCII 코드 (98) = -1
- 차이가 1이나 0, -25가 아니므로, current를 증가시키지 않음.
- str1의 세 번째 문자 'z'와 str2의 두 번째 문자 'a'를 비교.
- 차이: 'a'의 ASCII 코드 (97) - 'z'의 ASCII 코드 (122) = -25
- 차이가 -25이므로, current를 2로 증가
- current가 str2의 길이 2와 같으므로, true를 반환
'LeetCode' 카테고리의 다른 글
| 2615. Sum of Distances / TypeScript (0) | 2024.08.25 |
|---|---|
| 1409. Queries on a Permutation With Key / TypeScript (0) | 2024.08.21 |
| 698. Partition to K Equal Sum Subsets / TypeScript (0) | 2024.08.18 |
| 435. Non-overlapping Intervals / TypeScript (0) | 2024.08.15 |
| 2966. Divide Array Into Arrays With Max Difference (0) | 2024.08.13 |