File tree 2 files changed +43
-1
lines changed
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,356 LeetCode solutions in JavaScript
1
+ # 1,357 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1178
1178
1539|[ Kth Missing Positive Number] ( ./solutions/1539-kth-missing-positive-number.js ) |Easy|
1179
1179
1540|[ Can Convert String in K Moves] ( ./solutions/1540-can-convert-string-in-k-moves.js ) |Medium|
1180
1180
1541|[ Minimum Insertions to Balance a Parentheses String] ( ./solutions/1541-minimum-insertions-to-balance-a-parentheses-string.js ) |Medium|
1181
+ 1542|[ Find Longest Awesome Substring] ( ./solutions/1542-find-longest-awesome-substring.js ) |Hard|
1181
1182
1550|[ Three Consecutive Odds] ( ./solutions/1550-three-consecutive-odds.js ) |Easy|
1182
1183
1551|[ Minimum Operations to Make Array Equal] ( ./solutions/1551-minimum-operations-to-make-array-equal.js ) |Medium|
1183
1184
1566|[ Detect Pattern of Length M Repeated K or More Times] ( ./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1542. Find Longest Awesome Substring
3
+ * https://leetcode.com/problems/find-longest-awesome-substring/
4
+ * Difficulty: Hard
5
+ *
6
+ * You are given a string s. An awesome substring is a non-empty substring of s such that we
7
+ * can make any number of swaps in order to make it a palindrome.
8
+ *
9
+ * Return the length of the maximum length awesome substring of s.
10
+ */
11
+
12
+ /**
13
+ * @param {string } s
14
+ * @return {number }
15
+ */
16
+ var longestAwesome = function ( s ) {
17
+ let result = 0 ;
18
+ let prefixMask = 0 ;
19
+ const digitMasks = new Map ( ) ;
20
+ digitMasks . set ( 0 , - 1 ) ;
21
+
22
+ for ( let i = 0 ; i < s . length ; i ++ ) {
23
+ const digit = s . charCodeAt ( i ) - '0' . charCodeAt ( 0 ) ;
24
+ prefixMask ^= ( 1 << digit ) ;
25
+
26
+ if ( digitMasks . has ( prefixMask ) ) {
27
+ result = Math . max ( result , i - digitMasks . get ( prefixMask ) ) ;
28
+ } else {
29
+ digitMasks . set ( prefixMask , i ) ;
30
+ }
31
+
32
+ for ( let j = 0 ; j < 10 ; j ++ ) {
33
+ const oddMask = prefixMask ^ ( 1 << j ) ;
34
+ if ( digitMasks . has ( oddMask ) ) {
35
+ result = Math . max ( result , i - digitMasks . get ( oddMask ) ) ;
36
+ }
37
+ }
38
+ }
39
+
40
+ return result ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments