Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

(Java - C++ - Python) One Pass O (N) - LeetCode Discuss

This document presents an O(N) algorithm to count the number of unique characters in all substrings of a given string in one pass. It first provides an intuition using an example string to show how to count the number of ways a character can be found as unique in a substring. It then explains the algorithm which uses an index array to track the last two occurrence indexes of each character. It loops through the string, updates the indexes, and counts the number of ways each character can be unique based on the difference between its occurrence indexes. The overall complexity is O(N).

Uploaded by

nemesis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

(Java - C++ - Python) One Pass O (N) - LeetCode Discuss

This document presents an O(N) algorithm to count the number of unique characters in all substrings of a given string in one pass. It first provides an intuition using an example string to show how to count the number of ways a character can be found as unique in a substring. It then explains the algorithm which uses an index array to track the last two occurrence indexes of each character. It loops through the string, updates the indexes, and counts the number of ways each character can be unique based on the difference between its occurrence indexes. The overall complexity is O(N).

Uploaded by

nemesis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

23/07/2022, 19:06 [Java/C++/Python] One pass O(N) - LeetCode Discuss

Explore Problems InterviewNew Contest Discuss Store


Description Solution Discuss (362) Submissions
Back [Java/C++/Python] One pass O(N)
lee215 154038 Last Edit: May 2, 2022 6:54 PM 53.4K VIEWS
981
Foreword
Quite similar to the question in 2262. Total Appeal of A String,
which I think slightly simpler than this question.
I also suggest you practicing that first.
I have the similar intuition and approach for both questions.

Intuition
Let's think about how a character can be found as a unique character.
Think about string "XAXAXXAX" and focus on making the second "A" a unique character.
We can take "XA(XAXX)AX" and between "()" is our substring.
We can see here, to make the second "A" counted as a uniq character, we need to:
1. insert "(" somewhere between the first and second A
2. insert ")" somewhere between the second and third A
For step 1 we have "A(XA" and "AX(A" , 2 possibility.
For step 2 we have "A)XXA" , "AX)XA" and "AXX)A" , 3 possibilities.
So there are in total 2 * 3 = 6 ways to make the second A a unique character in a substring.
In other words, there are only 6 substring, in which this A contribute 1 point as unique string.
Instead of counting all unique characters and struggling with all possible substrings,
we can count for every char in S, how many ways to be found as a unique char.
We count and sum, and it will be out answer.

Explanation
1. index[26][2] record last two occurrence index for every upper characters.
2. Initialise all values in index to -1 .
3. Loop on string S, for every character c , update its last two occurrence index to index[c] .
4. Count when loop. For example, if "A" appears twice at index 3, 6, 9 seperately, we need to cou
For the first "A": (6-3) * (3-(-1))"
For the second "A": (9-6) * (6-3)"
For the third "A": (N-9) * (9-6)"

Complexity
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/128952/JavaC++Python-One-pass-O(N) 1/1

You might also like