Skip to content

Commit d5ed79f

Browse files
committed
Add solution #1585
1 parent ea49537 commit d5ed79f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,385 LeetCode solutions in JavaScript
1+
# 1,386 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1210,6 +1210,7 @@
12101210
1582|[Special Positions in a Binary Matrix](./solutions/1582-special-positions-in-a-binary-matrix.js)|Easy|
12111211
1583|[Count Unhappy Friends](./solutions/1583-count-unhappy-friends.js)|Medium|
12121212
1584|[Min Cost to Connect All Points](./solutions/1584-min-cost-to-connect-all-points.js)|Medium|
1213+
1585|[Check If String Is Transformable With Substring Sort Operations](./solutions/1585-check-if-string-is-transformable-with-substring-sort-operations.js)|Hard|
12131214
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12141215
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12151216
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1585. Check If String Is Transformable With Substring Sort Operations
3+
* https://leetcode.com/problems/check-if-string-is-transformable-with-substring-sort-operations/
4+
* Difficulty: Hard
5+
*
6+
* Given two strings s and t, transform string s into string t using the following operation any
7+
* number of times:
8+
* - Choose a non-empty substring in s and sort it in place so the characters are in ascending
9+
* order.
10+
* - For example, applying the operation on the underlined substring in "14234" results
11+
* in "12344".
12+
*
13+
* Return true if it is possible to transform s into t. Otherwise, return false.
14+
*
15+
* A substring is a contiguous sequence of characters within a string.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @param {string} t
21+
* @return {boolean}
22+
*/
23+
var isTransformable = function(source, target) {
24+
const digitPositions = Array.from({ length: 10 }, () => []);
25+
for (let index = 0; index < source.length; index++) {
26+
digitPositions[source[index]].push(index);
27+
}
28+
29+
const currentIndices = new Array(10).fill(0);
30+
31+
for (const digit of target) {
32+
const digitValue = parseInt(digit, 10);
33+
if (currentIndices[digitValue] >= digitPositions[digitValue].length) {
34+
return false;
35+
}
36+
37+
const position = digitPositions[digitValue][currentIndices[digitValue]];
38+
for (let smallerDigit = 0; smallerDigit < digitValue; smallerDigit++) {
39+
if (currentIndices[smallerDigit] < digitPositions[smallerDigit].length
40+
&& digitPositions[smallerDigit][currentIndices[smallerDigit]] < position) {
41+
return false;
42+
}
43+
}
44+
45+
currentIndices[digitValue]++;
46+
}
47+
48+
return true;
49+
};

0 commit comments

Comments
 (0)