<p>On a <strong>single-threaded</strong> CPU, we execute a program containing <code>n</code> functions. Each function has a unique ID between <code>0</code> and <code>n-1</code>.</p>

<p>Function calls are <strong>stored in a <a href="https://en.wikipedia.org/wiki/Call_stack">call stack</a></strong>: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is <strong>the current function being executed</strong>. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.</p>

<p>You are given a list <code>logs</code>, where <code>logs[i]</code> represents the <code>i<sup>th</sup></code> log message formatted as a string <code>&quot;{function_id}:{&quot;start&quot; | &quot;end&quot;}:{timestamp}&quot;</code>. For example, <code>&quot;0:start:3&quot;</code> means a function call with function ID <code>0</code> <strong>started at the beginning</strong> of timestamp <code>3</code>, and <code>&quot;1:end:2&quot;</code> means a function call with function ID <code>1</code> <strong>ended at the end</strong> of timestamp <code>2</code>. Note that a function can be called <b>multiple times, possibly recursively</b>.</p>

<p>A function&#39;s <strong>exclusive time</strong> is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for <code>2</code> time units and another call executing for <code>1</code> time unit, the <strong>exclusive time</strong> is <code>2 + 1 = 3</code>.</p>

<p>Return <em>the <strong>exclusive time</strong> of each function in an array, where the value at the </em><code>i<sup>th</sup></code><em> index represents the exclusive time for the function with ID </em><code>i</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" style="width: 550px; height: 239px;" />
<pre>
<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;1:start:2&quot;,&quot;1:end:5&quot;,&quot;0:end:6&quot;]
<strong>Output:</strong> [3,4]
<strong>Explanation:</strong>
Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;0:start:6&quot;,&quot;0:end:6&quot;,&quot;0:end:7&quot;]
<strong>Output:</strong> [8]
<strong>Explanation:</strong>
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
Function 0 (initial call) resumes execution then immediately calls itself again.
Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;1:start:6&quot;,&quot;1:end:6&quot;,&quot;0:end:7&quot;]
<strong>Output:</strong> [7,1]
<strong>Explanation:</strong>
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
Function 0 (initial call) resumes execution then immediately calls function 1.
Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.
Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.
So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= logs.length &lt;= 500</code></li>
	<li><code>0 &lt;= function_id &lt; n</code></li>
	<li><code>0 &lt;= timestamp &lt;= 10<sup>9</sup></code></li>
	<li>No two start events will happen at the same timestamp.</li>
	<li>No two end events will happen at the same timestamp.</li>
	<li>Each function has an <code>&quot;end&quot;</code> log for each <code>&quot;start&quot;</code> log.</li>
</ul>

class Solution {
public:


    vector<int> solve(string &log,bool &start){
        vector<int> v;
        int n=log.length();
        int i=0;
        int id=0;
        int val=0;
        string str="";
        while(log[i]!=':'){
            id=(id*10)+(log[i]-'0');
            i++;
        }
        i++;
        while(log[i]!=':'){
            str+=log[i];
            i++;
        }
        i++;
        if(str=="start") {
            start=true;
        }
        else{
            start=false;
        }
        while(i<n){
            val=(val*10)+(log[i]-'0');
            i++;
        }

        if(start){
            v.push_back(id);
            v.push_back(val);
        }
        else{
            v.push_back(id);
            v.push_back(val+1);
        }
        return v;

    }

    vector<int> exclusiveTime(int n, vector<string>& logs) {
       vector<int> ans(n,0);
        stack<int> st;
        int prev=0;
        for(string log:logs){
            vector<int> v;
            bool start=true;
            v=solve(log,start);
            if(start){
               
               if(st.size()!=0) ans[st.top()]+=v[1]-prev;
                 st.push(v[0]);
            }
            else{
                ans[st.top()]+=v[1]-prev;
                st.pop();
            }
           
            prev=v[1];
        }
        return ans;
    }
// };
// class Solution {
// public:
    // vector<int> solve(const string &log, bool &start) {
    //     vector<int> v;
    //     int i = 0;
    //     int id = 0;
    //     int val = 0;
    //     string type = "";
        
    //     // Parse the ID
    //     while (log[i] != ':') {
    //         id = (id * 10) + (log[i] - '0');
    //         i++;
    //     }
    //     i++; // Skip the colon

    //     // Parse the type (start or end)
    //     while (log[i] != ':') {
    //         type += log[i];
    //         i++;
    //     }
    //     i++; // Skip the colon

    //     // Set whether this is a "start" or "end"
    //     start = (type == "start");

    //     // Parse the timestamp
    //     while (i < log.length()) {
    //         val = (val * 10) + (log[i] - '0');
    //         i++;
    //     }

    //     if (start) {
    //         v.push_back(id);
    //         v.push_back(val);
    //     } else {
    //         v.push_back(id);
    //         v.push_back(val + 1);  // Add 1 to include the current second for "end"
    //     }

    //     return v;
    // }

    // // vector<int> exclusiveTime(int n, vector<string>& logs) {
    //     vector<int> ans(n, 0);
    //     stack<int> st;
    //     int prev = 0;
        
    //     for (const string &log : logs) {
    //         vector<int> v;
    //         bool start;
    //         v = solve(log, start);
            
    //         if (start) {
    //             if (!st.empty()) {
    //                 // Add the time between the previous start/end and current start
    //                 ans[st.top()] += v[1] - prev;
    //             }
    //             st.push(v[0]);  // Push the current function ID
    //         } else {
    //             // Add the time between the previous start/end and current end (inclusive)
    //             ans[st.top()] += v[1] - prev;
    //             st.pop();
    //         }

    //         prev = v[1];  // Update prev to the current timestamp
    //     }

    //     return ans;
    // }
};

20100