Skip to content

Commit e04d273

Browse files
committed
Add solution #467
1 parent ba202e3 commit e04d273

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@
374374
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|
375375
464|[Can I Win](./0464-can-i-win.js)|Medium|
376376
466|[Count The Repetitions](./0466-count-the-repetitions.js)|Hard|
377+
467|[Unique Substrings in Wraparound String](./0467-unique-substrings-in-wraparound-string.js)|Medium|
377378
472|[Concatenated Words](./0472-concatenated-words.js)|Hard|
378379
476|[Number Complement](./0476-number-complement.js)|Easy|
379380
482|[License Key Formatting](./0482-license-key-formatting.js)|Easy|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 467. Unique Substrings in Wraparound String
3+
* https://leetcode.com/problems/unique-substrings-in-wraparound-string/
4+
* Difficulty: Medium
5+
*
6+
* We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz",
7+
* so base will look like this:
8+
* - "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".
9+
*
10+
* Given a string s, return the number of unique non-empty substrings of s are present in base.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {number}
16+
*/
17+
var findSubstringInWraproundString = function(s) {
18+
const maxLength = new Array(26).fill(0);
19+
let count = 0;
20+
21+
for (let i = 0; i < s.length; i++) {
22+
count = i > 0 && (s.charCodeAt(i) - s.charCodeAt(i - 1) - 1) % 26 === 0 ? count + 1 : 1;
23+
const index = s.charCodeAt(i) - 97;
24+
maxLength[index] = Math.max(maxLength[index], count);
25+
}
26+
27+
return maxLength.reduce((sum, n) => sum + n, 0);
28+
};

0 commit comments

Comments
 (0)