kamyu104 / LeetCode-Solutions

🏋️ Python / Modern C++ Solutions of All 3292 LeetCode Problems (Weekly Update)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Explanation for 1358 number of substrings containing all characters

sparshgarg23 opened this issue · comments

I was going through your code for 1358 no of substrings containing all 3 characters.
I am assuming that the purpose of the left variable is to keep track of where a,b,c occur in the string.
We then choose the min of those indices,and add min_index+1 to the count.
We do this for all chars in string and then return the final count.
Is that correct?
Detailed explanation is here
https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/516955/Java-ELEGANT-No-Sliding-Window-EXPLAINED-(No-of-Sub-Strings-Ending-at-Curr-Index)

Also,if possible can you please add some explanation /reasoning for the codes.I know it will be difficult,but it helps us to understand how you came at the solution.

commented
  • About min(left)
    • Yes, min(left) is to keep track of the rightmost begin position of valid substrings which end at position right.
    • Given position right, [s[i:right+1] for i in range(min(left)+1)]] are all valid substrings.
  • About explanations
    • This repository is just a place to keep my practice code and some notes.
    • Writing code explanations takes too much time than writing solutions.
    • Currently, I only have time to write some comments on what I consider difficult solutions.