We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Special String Again
- Discussions
Special String Again
Special String Again
Sort by
recency
|
787 Discussions
|
Please Login in order to post a comment
prefix sum algorithm
`long substrCount(int n, string s) { vector left(n, 0), right(n, 0); left[0] = 1; right[n - 1] = 1; for(int i = 1; i < s.size(); i++) { if(s[i] == s[i - 1]){ left[i] = left[i - 1] + 1; } else { left[i] = 1; } } for(int i = n - 1; i >= 0; i--) { if(s[i] == s[i + 1]){ right[i] = right[i + 1] + 1; } else { right[i] = 1; } }
}
JS - fast
(hopefully) easy-to-read O(n) Python solution
I use a deque to keep track of previous counts to find the middle cases. I like this solution because there is a single loop while most other solutions have nested loops.
import { WriteStream, createWriteStream } from 'fs'; import { stdin, stdout } from 'process';
// Function to count special substrings function substrCount(n: number, s: string): number { let count = 0;
}
// Main function to handle input and output function main() { const inputLines: string[] = [];
}
main();