Gegeven een touwtje str met lengte n (1<= n <= 106) en een nummer k de taak is om het k-de niet-herhalende teken in de string te vinden.
Java-kaart voorbeeld
Voorbeelden:
Invoer: str = geeksforgeeks k = 3
Uitgang: R
Uitleg: Het eerste niet-herhalende teken is f, het tweede is o en het derde is r.Invoer: str = geeksforgeeks k = 2
Uitgang: dec reeks tekenreeksenInvoer : str = geeksforgeeks k = 4
Uitgang: Minder dan k niet-herhalende tekens in invoer.
Dit probleem is voornamelijk een uitbreiding van de Eerste niet-herhalend karakterprobleem .
K'th niet-herhalend karakter met behulp van geneste lus:
Een eenvoudige oplossing is om twee lussen uit te voeren. Begin met het doorkruisen vanaf de linkerkant. Controleer voor elk teken of het wordt herhaald of niet. Als het teken niet wordt herhaald, wordt het aantal niet-herhalende tekens verhoogd. Toen de mede in nt wordt k geef het karakter terug.
Hieronder ziet u de implementatie van het bovenstaande idee:
C++
// Include all standard libraries #include using namespace std; // Define a function to find the kth non-repeating character // in a string char kthNonRepeatingChar(string str int k) { // Initialize count and result variables to 0 and null // character respectively int count = 0; char result = ' '; // Loop through each character in the string for (int i = 0; i < str.length(); i++) { // Assume that the current character does not repeat bool repeating = false; // Loop through the rest of the string to check if // the current character repeats for (int j = 0; j < i; j++) { if(str[j]==str[i]) { repeating = true; } } for (int j = i + 1; j < str.length(); j++) { if (str[i] == str[j]) { // If the current character repeats set the // repeating flag to true and exit the loop repeating = true; } } // If the current character does not repeat // increment the count of non-repeating characters if (!repeating) { count++; // If the count of non-repeating characters // equals k set the result variable to the // current character and exit the loop if (count == k) { result = str[i]; break; } } } // Return the result variable return result; } // Define the main function to test the kthNonRepeatingChar // function int main() { // Define an example string and value of k string str = 'geeksforgeeks'; int k = 3; // Call the kthNonRepeatingChar function with the // example string and value of k char result = kthNonRepeatingChar(str k); // Check if the result variable contains a non-null // character and print the appropriate message if (result == ' ') { cout << 'There is no kth non-repeating character ' 'in the string.n'; } else { cout << 'The ' << k << 'th non-repeating character in the string ' 'is ' << result << '.n'; } // End the program return 0; }
Java // Import required libraries import java.util.*; // Define a class to find the kth non-repeating character // in a string public class Main { public static char kthNonRepeatingChar(String str int k) { // Initialize count and result variables to 0 and // null character respectively int count = 0; char result = ' '; // Loop through each character in the string for (int i = 0; i < str.length(); i++) { // Assume that the current character does not // repeat boolean repeating = false; // Loop through the rest of the string to check // if the current character repeats for (int j = i + 1; j < str.length(); j++) { if (str.charAt(i) == str.charAt(j)) { // If the current character repeats set // the repeating flag to true and exit // the loop repeating = true; break; } } // If the current character does not repeat // increment the count of non-repeating // characters if (!repeating) { count++; // If the count of non-repeating characters // equals k set the result variable to the // current character and exit the loop if (count == k) { result = str.charAt(i); break; } } } // Return the result variable return result; } // Define the main function to test the // kthNonRepeatingChar function public static void main(String[] args) { // Define an example string and value of k String str = 'geeksforgeeks'; int k = 3; // Call the kthNonRepeatingChar function with the // example string and value of k char result = kthNonRepeatingChar(str k); // Check if the result variable contains a non-null // character and print the appropriate message if (result == ' ') { System.out.println( 'There is no kth non-repeating character ' + 'in the string.'); } else { System.out.println( 'The ' + k + 'th non-repeating character in the string ' + 'is ' + result + '.'); } } }
Python # Define a function to find the kth non-repeating character # in a string def kthNonRepeatingChar(s: str k: int) -> str: # Initialize count and result variables to 0 and null # character respectively count = 0 result = '