Skip to content

Commit 75d81c9

Browse files
Add files via upload
1 parent ca1e859 commit 75d81c9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 附上大佬的解题思路,确实是太秀了
2+
// https://leetcode.com/problems/unique-substrings-in-wraparound-string/discuss/95439/Concise-Java-solution-using-DP
3+
4+
// Runtime: 8 ms, faster than 91.38% of C++ online submissions for Unique Substrings in Wraparound String.
5+
// Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Unique Substrings in Wraparound String.
6+
7+
class Solution
8+
{
9+
public:
10+
int findSubstringInWraproundString(string p)
11+
{
12+
vector<int> memo(26, 0);
13+
14+
int curLength = 1;
15+
for (int i = 0; i < p.length(); ++i)
16+
{
17+
if (i > 0 && p[i] - 'a' == (p[i - 1] - 'a' + 1) % 26)
18+
{
19+
++curLength;
20+
}
21+
else
22+
{
23+
curLength = 1;
24+
}
25+
memo[p[i] - 'a'] = max(memo[p[i] - 'a'], curLength);
26+
}
27+
28+
int res = 0;
29+
for (auto item : memo)
30+
{
31+
res += item;
32+
}
33+
return res;
34+
}
35+
};

0 commit comments

Comments
 (0)