✅☑[C++/C/Java/Python/JavaScript] || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n1. The lastVisitedIntegers function takes a vector of strings as input, where
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. The `lastVisitedIntegers` function takes a vector of strings as input, where each string represents either an integer or the word "prev."\n\n1. It initializes two vectors: `ov` to store visited integers and `ans` to store the answers.\n\n1. `ci` is the current index variable, initialized to -1.\n\n1. It iterates through each string in the input `words`.\n\n1. If the current word is "prev," it checks whether there is a previous integer in `ov`:\n\n    - If there is no previous integer (ci == -1), it pushes -1 to `ans` to indicate that there is no previous integer.\n    - If there is a previous integer, it pushes the previous integer from `ov` to `ans` and decrements the current index `ci`.\n1. If the current word is not `prev`, it converts the string to an integer value:\n\n    - It initializes `vv` to 0 to store the integer value.\n    - It iterates through the characters of the string, converting them to digits and accumulating the integer value.\n1. After processing each string, it updates the current index `ci` to the last index in the `ov` vector, indicating that the most recently added integer is the current one.\n\n1. The integer value is added to the `ov` vector.\n\n1. Finally, the function returns the `ans` vector containing the answers, which indicate the previous integers corresponding to "prev" words in the input.\n\n\n# Complexity\n- *Time complexity:*\n   $$O(N * M)$$\n    \n\n- *Space complexity:*\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    vector<int> lastVisitedIntegers(vector<string>& words) {\n        vector<int> ov; // Create a vector to store the visited integers.\n        vector<int> ans; // Create a vector to store the answers.\n        int ci = -1; // Initialize a current index variable to -1.\n\n        for (string s : words) {\n            if (s == "prev") { // If the word is "prev," we need to handle the previous integer.\n                if (ci == -1) {\n                    ans.push_back(-1); // If there is no previous integer, push -1 to the answers.\n                }\n                else {\n                    ans.push_back(ov[ci--]); // Otherwise, push the previous integer and decrement the current index.\n                }\n            }\n            else {\n                int vv = 0; // Initialize a variable to store the current integer value.\n                for (char c : s) {\n                    vv *= 10; // Shift digits one position left.\n                    vv += (c - \'0\'); // Add the current digit value to vv.\n                }\n                ci = ov.size(); // Update the current index to the last index in ov.\n                ov.push_back(vv); // Add the integer to the visited integers vector.\n            }\n        }\n        return ans; // Return the vector containing the answers.\n    }\n};\n\n\n```\n\n\n```C []\n\n#include <stdio.h>\n#include <stdlib.h>\n\nint* lastVisitedIntegers(char** words, int n) {\n    int* ov = (int*)malloc(n * sizeof(int)); // Create an array to store the visited integers.\n    int* ans = (int*)malloc(n * sizeof(int)); // Create an array to store the answers.\n    int ci = -1; // Initialize a current index variable to -1.\n\n    for (int i = 0; i < n; i++) {\n        char* s = words[i];\n        if (strcmp(s, "prev") == 0) { // If the word is "prev," we need to handle the previous integer.\n            if (ci == -1) {\n                ans[i] = -1; // If there is no previous integer, store -1 in the answers.\n            } else {\n                ans[i] = ov[ci--]; // Otherwise, store the previous integer and decrement the current index.\n            }\n        } else {\n            int vv = 0; // Initialize a variable to store the current integer value.\n            for (int j = 0; s[j] != \'\\0\'; j++) {\n                vv *= 10; // Shift digits one position left.\n                vv += (s[j] - \'0\'); // Add the current digit value to vv.\n            }\n            ci = i; // Update the current index to the current position in ov.\n            ov[i] = vv; // Store the integer in the visited integers array.\n        }\n    }\n    return ans; // Return the array containing the answers.\n}\n\n\n```\n\n\n\n```Java []\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Solution {\n    public List<Integer> lastVisitedIntegers(List<String> words) {\n        List<Integer> ov = new ArrayList<>();\n        List<Integer> ans = new ArrayList<>();\n        int ci = -1;\n\n        for (String s : words) {\n            if (s.equals("prev")) {\n                if (ci == -1) {\n                    ans.add(-1);\n                } else {\n                    ans.add(ov.get(ci--));\n                }\n            } else {\n                int vv = 0;\n                for (char c : s.toCharArray()) {\n                    vv *= 10;\n                    vv += (c - \'0\');\n                }\n                ci = ov.size();\n                ov.add(vv);\n            }\n        }\n        return ans;\n    }\n}\n\n\n```\n\n\n```python3 []\nclass Solution:\n    def lastVisitedIntegers(self, words):\n        ov = []\n        ans = []\n        ci = -1\n\n        for s in words:\n            if s == "prev":\n                if ci == -1:\n                    ans.append(-1)\n                else:\n                    ans.append(ov[ci])\n                    ci -= 1\n            else:\n                vv = 0\n                for c in s:\n                    vv *= 10\n                    vv += int(c)\n                ci = len(ov)\n                ov.append(vv)\n\n        return ans\n\n\n```\n\n```javascript []\n\nclass Solution {\n    lastVisitedIntegers(words) {\n        const ov = [];\n        const ans = [];\n        let ci = -1;\n\n        for (const s of words) {\n            if (s === "prev") {\n                if (ci === -1) {\n                    ans.push(-1);\n                } else {\n                    ans.push(ov[ci]);\n                    ci--;\n                }\n            } else {\n                let vv = 0;\n                for (const c of s) {\n                    vv *= 10;\n                    vv += parseInt(c);\n                }\n                ci = ov.length;\n                ov.push(vv);\n            }\n        }\n        return ans;\n    }\n}\n\n\n```\n\n\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['String', 'C', 'String Matching', 'C++', 'Java', 'Python3', 'JavaScript']