Given two strings s1 and s2 containing lowercase English letters, return the smallest index of a character in s1 that is also present in s2. If no common character exists between the two strings, return -1.
Examples:Â
Input: s1 = "geeksforgeeks", s2 = "set"Â
Output: 1Â
Explanation: The character 'e' is present in both s1 and s2. Its first occurrence in s1 is at index 1, which is the minimum such index.Input: s1 = "geeks", s2= "hop"Â
Output: -1
Explanation: There is no character that is common to both s1 and s2. Hence, the answer is -1.
Source: OLA Interview Experience
Table of Content
[Naive Approach] Using Two Nested Loops - O(m*n) Time and O(1) Space
Check each character of s1 from left to right and find the first one that is also present in s2.
- Traverse each character of s1 one by one.
- For every character in s1, scan s2 to check if it is present.
- Return the index of the first matching character.
- If no common character is found, return -1.
#include <bits/stdc++.h>
using namespace std;
int minIndexChar(const string &s1, const string &s2) {
// Iterate over each character in s1
for (int i = 0; i < s1.length(); i++) {
// Check if the current character exists in s2
for (int j = 0; j < s2.length(); j++) {
// If a match is found, return its index
if (s1[i] == s2[j]) {
return i;
}
}
}
// If no common character is found
return -1;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "set";
cout << minIndexChar(s1, s2);
return 0;
}
class GFG {
public static int minIndexChar(String s1, String s2) {
// Iterate over each character in s1
for (int i = 0; i < s1.length(); i++) {
// Check if the current character exists in s2
for (int j = 0; j < s2.length(); j++) {
// If a match is found, return its index
if (s1.charAt(i) == s2.charAt(j)) {
return i;
}
}
}
// If no common character is found
return -1;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "set";
System.out.println(minIndexChar(s1, s2));
}
}
def minIndexChar(s1, s2):
# Iterate over each character in s1
for i in range(len(s1)):
# Check if the current character exists in s2
for j in range(len(s2)):
# If a match is found, return its index
if s1[i] == s2[j]:
return i
# If no common character is found
return -1
s1 = "geeksforgeeks"
s2 = "set"
print(minIndexChar(s1, s2))
using System;
public class GFG{
public static int minIndexChar(string s1, string s2){
// Iterate over each character in s1
for (int i = 0; i < s1.Length; i++){
// Check if the current character exists in s2
for (int j = 0; j < s2.Length; j++){
// If a match is found, return its index
if (s1[i] == s2[j]){
return i;
}
}
}
// If no common character is found
return -1;
}
public static void Main(){
string s1 = "geeksforgeeks";
string s2 = "set";
Console.WriteLine(minIndexChar(s1, s2));
}
}
function minIndexChar(s1, s2) {
// Iterate over each character in s1
for (let i = 0; i < s1.length; i++) {
// Check if the current character exists in s2
for (let j = 0; j < s2.length; j++) {
// If a match is found, return its index
if (s1[i] === s2[j]) {
return i;
}
}
}
// If no common character is found
return -1;
}
// Driver code
let s1 = "geeksforgeeks";
let s2 = "set";
console.log(minIndexChar(s1, s2));
Output
1
[Expected Approach 1] Using Frequency Array - O(n) Time and O(1) Space
Store the characters of s2 in frequency array for quick lookup, then find the first character in s1 that is also present in s2.
- Create a frequency array of size 26 and mark the characters present in s2.
- Traverse s1 from left to right.
- If the current character is marked in the frequency array, return its index.
- If no common character is found, return -1.
#include <bits/stdc++.h>
using namespace std;
int minIndexChar(string &s1, string &s2) {
// Create a frequency array
// to mark characters present in s2
vector<int> hash(26, 0);
// Mark all characters of s2
for (char ch : s2)
hash[ch - 'a']++;
// Traverse s1 and return the first matching index
for (int i = 0; i < s1.length(); i++) {
if (hash[s1[i] - 'a'])
return i;
}
// If no common character is found
return -1;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "set";
cout << minIndexChar(s1, s2) << endl;
return 0;
}
import java.util.Arrays;
public class GFG {
static int minIndexChar(String s1, String s2) {
// Create a frequency array
// to mark characters present in s2
int[] hash = new int[26];
Arrays.fill(hash, 0);
// Mark all characters of s2
for (char ch : s2.toCharArray())
hash[ch - 'a']++;
// Traverse s1 and return the first matching index
for (int i = 0; i < s1.length(); i++) {
if (hash[s1.charAt(i) - 'a'] > 0)
return i;
}
// If no common character is found
return -1;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "set";
System.out.println(minIndexChar(s1, s2));
}
}
def minIndexChar(s1, s2):
# Create a frequency array
# to mark characters present in s2
hash = [0] * 26
# Mark all characters of s2
for ch in s2:
hash[ord(ch) - ord('a')] += 1
# Traverse s1 and return the first matching index
for i in range(len(s1)):
if hash[ord(s1[i]) - ord('a')] > 0:
return i
# If no common character is found
return -1
if __name__ == '__main__':
s1 = "geeksforgeeks"
s2 = "set"
print(minIndexChar(s1, s2))
using System;
public class GFG {
static int minIndexChar(string s1, string s2) {
// Create a frequency array
// to mark characters present in s2
int[] hash = new int[26];
// Mark all characters of s2
foreach (char ch in s2)
hash[ch - 'a']++;
// Traverse s1 and return the first matching index
for (int i = 0; i < s1.Length; i++) {
if (hash[s1[i] - 'a'] > 0)
return i;
}
// If no common character is found
return -1;
}
public static void Main() {
string s1 = "geeksforgeeks";
string s2 = "set";
Console.WriteLine(minIndexChar(s1, s2));
}
}
function minIndexChar(s1, s2) {
// Create a frequency array
// to mark characters present in s2
let hash = new Array(26).fill(0);
// Mark all characters of s2
for (let ch of s2) {
hash[ch.charCodeAt(0) - 'a'.charCodeAt(0)]++;
}
// Traverse s1 and return the first matching index
for (let i = 0; i < s1.length; i++) {
if (hash[s1.charCodeAt(i) - 'a'.charCodeAt(0)] > 0) {
return i;
}
}
// If no common character is found
return -1;
}
// Driver code
let s1 = "geeksforgeeks";
let s2 = "set";
console.log(minIndexChar(s1, s2));
Output
1
[Expected Approach 2] Using Hash Set - O(n) Time and O(1) space
Store all characters of s2 in a hash-based data structure for fast lookups, then find the first matching character in s1.
- Insert all characters of s2 into a hash set.
- Traverse s1 from left to right.
- If the current character exists in the hash set, return its index.
- If no common character is found, return -1.
#include <bits/stdc++.h>
using namespace std;
int minIndexChar(string &s1, string &s2) {
// Store all characters of s2 in a hash set
unordered_set<char> st;
for (char ch : s2)
st.insert(ch);
// Find the first character in s1 that is present in s2
for (int i = 0; i < s1.size(); i++) {
if (st.count(s1[i]))
return i;
}
return -1;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "set";
int result = minIndexChar(s1, s2);
cout << result << endl;
return 0;
}
import java.util.HashSet;
public class GFG {
public static int minIndexChar(String s1, String s2) {
HashSet<Character> st = new HashSet<>();
for (char ch : s2.toCharArray())
st.add(ch);
// Find the first character in s1 that is present in s2
for (int i = 0; i < s1.length(); i++) {
if (st.contains(s1.charAt(i)))
return i;
}
return -1;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "set";
int result = minIndexChar(s1, s2);
System.out.println(result);
}
}
def minIndexChar(s1, s2):
# Store all characters of s2 in a hash set
st = set()
for ch in s2:
st.add(ch)
# Find the first character in s1 that is present in s2
for i in range(len(s1)):
if s1[i] in st:
return i
return -1
if __name__ == "__main__":
s1 = "geeksforgeeks"
s2 = "set"
result = minIndexChar(s1, s2)
print(result)
using System;
using System.Collections.Generic;
public class GFG {
public static int minIndexChar(string s1, string s2) {
HashSet<char> st = new HashSet<char>();
foreach (char ch in s2)
st.Add(ch);
// Find the first character in s1 that is present in s2
for (int i = 0; i < s1.Length; i++) {
if (st.Contains(s1[i]))
return i;
}
return -1;
}
public static void Main() {
string s1 = "geeksforgeeks";
string s2 = "set";
int result = minIndexChar(s1, s2);
Console.WriteLine(result);
}
}
function minIndexChar(s1, s2) {
// Store all characters of s2 in a hash set
let st = new Set();
for (let ch of s2)
st.add(ch);
// Find the first character in s1 that is present in s2
for (let i = 0; i < s1.length; i++) {
if (st.has(s1[i]))
return i;
}
return -1;
}
// Driver code
let s1 = "geeksforgeeks";
let s2 = "set";
let result = minIndexChar(s1, s2);
console.log(result);
Output
1