File tree 2 files changed +51
-1
lines changed
2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,385 LeetCode solutions in JavaScript
1
+ # 1,386 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1210
1210
1582|[ Special Positions in a Binary Matrix] ( ./solutions/1582-special-positions-in-a-binary-matrix.js ) |Easy|
1211
1211
1583|[ Count Unhappy Friends] ( ./solutions/1583-count-unhappy-friends.js ) |Medium|
1212
1212
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|
1213
1214
1598|[ Crawler Log Folder] ( ./solutions/1598-crawler-log-folder.js ) |Easy|
1214
1215
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
1215
1216
1668|[ Maximum Repeating Substring] ( ./solutions/1668-maximum-repeating-substring.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments