|
| 1 | +--- |
| 2 | +id: minimum-indexed-character |
| 3 | +title: Minimum Indexed Character |
| 4 | +sidebar_label: 0024 Minimum Indexed Character |
| 5 | +tags: |
| 6 | +- String |
| 7 | +- Hashing |
| 8 | +- JavaScript |
| 9 | +- TypeScript |
| 10 | +- Python |
| 11 | +- Java |
| 12 | +- C++ |
| 13 | +description: "This document explores different approaches to solving the problem of finding the minimum index of a character in a string that is also present in another string, including solutions in JavaScript, TypeScript, Python, Java, and C++." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem |
| 17 | + |
| 18 | +Given a string `str` and another string `patt`, find the minimum index of the character in `str` that is also present in `patt`. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: |
| 26 | +str = "geeksforgeeks" |
| 27 | +patt = "set" |
| 28 | +
|
| 29 | +Output: |
| 30 | +1 |
| 31 | +
|
| 32 | +Explanation: |
| 33 | +'e' is the character which is present in the given string "geeksforgeeks" and is also present in "set". The minimum index of 'e' is 1. |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: |
| 40 | +str = "adcffaet" |
| 41 | +patt = "onkl" |
| 42 | +
|
| 43 | +Output: |
| 44 | +-1 |
| 45 | +
|
| 46 | +Explanation: |
| 47 | +There are no characters that are common in "patt" and "str". |
| 48 | +``` |
| 49 | + |
| 50 | +### Your Task |
| 51 | +You only need to complete the function `minIndexChar()` that returns the index of the answer in `str` or returns `-1` if no character of `patt` is present in `str`. |
| 52 | + |
| 53 | +**Expected Time Complexity:** O(N) |
| 54 | +**Expected Auxiliary Space:** O(Number of distinct characters) |
| 55 | + |
| 56 | +### Constraints |
| 57 | +- 1 ≤ |str|,|patt| ≤ 10^5 |
| 58 | +- 'a' ≤ str[i], patt[i] ≤ 'z' |
| 59 | + |
| 60 | +## Solution |
| 61 | + |
| 62 | +### Approach |
| 63 | + |
| 64 | +To solve this problem, we can follow these steps: |
| 65 | + |
| 66 | +1. Iterate through the characters in the `patt` string. |
| 67 | +2. For each character in `patt`, find its index in the `str` string using the `find` method. |
| 68 | +3. Store the indices in a vector. |
| 69 | +4. Iterate through the vector to find the minimum index that is not `-1` (indicating the character is found in `str`). |
| 70 | +5. If no valid index is found, return `-1`; otherwise, return the minimum index. |
| 71 | + |
| 72 | +### Implementation |
| 73 | + |
| 74 | +<Tabs> |
| 75 | + <TabItem value="cpp" label="C++"> |
| 76 | + |
| 77 | +```cpp |
| 78 | +class Solution |
| 79 | +{ |
| 80 | + public: |
| 81 | + int minIndexChar(string str, string patt) |
| 82 | + { |
| 83 | + int res = INT_MAX; |
| 84 | + vector<int> v1; |
| 85 | + for (int i = 0; i < patt.length(); i++) |
| 86 | + { |
| 87 | + int p = str.find(patt[i]); |
| 88 | + v1.push_back(p); |
| 89 | + } |
| 90 | + int min = INT_MAX; |
| 91 | + for (int i = 0; i < v1.size(); i++) |
| 92 | + { |
| 93 | + if (min > v1[i] && v1[i] != -1) |
| 94 | + min = v1[i]; |
| 95 | + } |
| 96 | + if (min == INT_MAX) |
| 97 | + return -1; |
| 98 | + return min; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | + </TabItem> |
| 104 | + <TabItem value="javascript" label="JavaScript"> |
| 105 | + |
| 106 | +```javascript |
| 107 | +function minIndexChar(str, patt) { |
| 108 | + let minIndex = Infinity; |
| 109 | + for (let char of patt) { |
| 110 | + let index = str.indexOf(char); |
| 111 | + if (index !== -1 && index < minIndex) { |
| 112 | + minIndex = index; |
| 113 | + } |
| 114 | + } |
| 115 | + return minIndex === Infinity ? -1 : minIndex; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | + </TabItem> |
| 120 | + <TabItem value="typescript" label="TypeScript"> |
| 121 | + |
| 122 | +```typescript |
| 123 | +function minIndexChar(str: string, patt: string): number { |
| 124 | + let minIndex = Infinity; |
| 125 | + for (let char of patt) { |
| 126 | + let index = str.indexOf(char); |
| 127 | + if (index !== -1 && index < minIndex) { |
| 128 | + minIndex = index; |
| 129 | + } |
| 130 | + } |
| 131 | + return minIndex === Infinity ? -1 : minIndex; |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | + </TabItem> |
| 136 | + <TabItem value="python" label="Python"> |
| 137 | + |
| 138 | +```python |
| 139 | +class Solution: |
| 140 | + def minIndexChar(self, str: str, patt: str) -> int: |
| 141 | + min_index = float('inf') |
| 142 | + for char in patt: |
| 143 | + index = str.find(char) |
| 144 | + if index != -1 and index < min_index: |
| 145 | + min_index = index |
| 146 | + return -1 if min_index == float('inf') else min_index |
| 147 | +``` |
| 148 | + |
| 149 | + </TabItem> |
| 150 | + <TabItem value="java" label="Java"> |
| 151 | + |
| 152 | +```java |
| 153 | +class Solution { |
| 154 | + public int minIndexChar(String str, String patt) { |
| 155 | + int minIndex = Integer.MAX_VALUE; |
| 156 | + for (char ch : patt.toCharArray()) { |
| 157 | + int index = str.indexOf(ch); |
| 158 | + if (index != -1 && index < minIndex) { |
| 159 | + minIndex = index; |
| 160 | + } |
| 161 | + } |
| 162 | + return minIndex == Integer.MAX_VALUE ? -1 : minIndex; |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | + </TabItem> |
| 168 | +</Tabs> |
| 169 | + |
| 170 | +### Complexity Analysis |
| 171 | + |
| 172 | +- **Time Complexity:** O(N), where N is the length of the string `str`. We iterate through each character in `patt` and use the `find` or `indexOf` method, which runs in O(N) time. |
| 173 | +- **Space Complexity:** O(1), as we only use a constant amount of extra space for variables. |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## References |
| 178 | + |
| 179 | +- **GeeksforGeeks Problem:** [Minimum Indexed Character](https://www.geeksforgeeks.org/minimum-indexed-character/) |
| 180 | +- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://www.geeksforgeeks.org/user/lakumvipwjge/) |
0 commit comments