My issue
Hidden Test cases cannot be passed
My code
import java.util.Scanner;
public class Main {
public static int findKthOccurrence(String s1, char c1, int k) {
int count = 0;
// Iterate through the string to find the k-th occurrence of c1
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == c1) {
count++;
if (count == k) {
return i + 1; // Return 1-based index
}
}
}
return -1; // If k-th occurrence doesn't exist
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read input
String s1 = sc.next();
char c1 = sc.next().charAt(0);
int k = sc.nextInt();
// Call the function and print the result
int result = findKthOccurrence(s1, c1, k);
System.out.println(result);
sc.close();
}
}
Learning course: Data structures & Algorithms lab
Problem Link: Find Kth Character Position in Data structures & Algorithms lab