File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Unique Substrings in Wraparound String Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments