Dada una string str y un entero X , la tarea es encontrar e imprimir las primeras X vocales de str . Si el total de vocales en str es <X , imprima -1 .
Ejemplos:
Entrada: str = “GeeksForGeeks”, X = 3
Salida: eeo
‘e’, ’e’ y ‘o’ son las primeras tres vocales en la string dada.Entrada: str = “softcopy”, X = 5
Salida: -1
El recuento total de vocales es 2, es decir, ‘o’ y ‘o’
Enfoque: recorrer la string carácter por carácter y comprobar si el carácter actual es una vocal. Si el carácter actual es una vocal, entonces concatene con la string resultante, result . Si en algún momento la longitud de la string resultante se convierte en X , imprima el resultado de la string ; de lo contrario, imprima -1 al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to check if a character is vowel bool isVowel(char c) { c = tolower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function to return first X vowels string firstXvowels(string s, int x) { // String to store first X vowels string result = ""; for (int i = 0; i < s.length(); i++) { // If s[i] is a vowel then // append it to the result if (isVowel(s[i])) result += s[i]; // If the desired length is reached if (result.length() == x) { return result; } } // If total vowels are < X return "-1"; } // Driver code int main() { string str = "GeeksForGeeks"; int x = 3; cout << firstXvowels(str, x); return 0; }
Java
// Java implementation of the approach public class GFG{ // Function to check if a character is vowel static boolean isVowel(char c) { c = Character.toLowerCase(c) ; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function to return first X vowels static String firstXvowels(String s, int x) { // String to store first X vowels String result = ""; for (int i = 0; i < s.length(); i++) { // If s[i] is a vowel then // append it to the result if (isVowel(s.charAt(i))) result += s.charAt(i); // If the desired length is reached if (result.length() == x) { return result; } } // If total vowels are < X return "-1"; } // Driver code public static void main(String []args) { String str = "GeeksForGeeks"; int x = 3; System.out.println(firstXvowels(str, x)) ; } // This code is contributed by Ryuga }
Python3
# Python 3 implementation of the approach # Function to check if a character is vowel def isVowel(c): c = c.lower() if (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'): return True return False # Function to return first X vowels def firstXvowels(s, x): # String to store first X vowels result = "" for i in range(0, len(s), 1): # If s[i] is a vowel then # append it to the result if (isVowel(s[i])): result += s[i] # If the desired length is reached if (len(result) == x): return result # If total vowels are < X return "-1" # Driver code if __name__ == '__main__': str = "GeeksForGeeks" x = 3 print(firstXvowels(str, x)) # This code is implemented by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to check if a // character is vowel static bool isVowel(char c) { c = Char.ToLower(c) ; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function to return first X vowels static string firstXvowels(string s, int x) { // String to store first X vowels string result = ""; for (int i = 0; i < s.Length; i++) { // If s[i] is a vowel then // append it to the result if (isVowel(s[i])) result += s[i]; // If the desired length is reached if (result.Length == x) { return result; } } // If total vowels are < X return "-1"; } // Driver code public static void Main() { string str = "GeeksForGeeks"; int x = 3; Console.WriteLine(firstXvowels(str, x)) ; } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP implementation of the approach // Function to check if a character is vowel function isVowel($c) { $c = strtolower($c); if ($c == 'a' || $c == 'e' || $c == 'i' || $c == 'o' || $c == 'u') return true; return false; } // Function to return first X vowels function firstXvowels($s, $x) { // String to store first X vowels $result = ""; for ($i = 0; $i < strlen($s); $i++) { // If s[i] is a vowel then // append it to the result if (isVowel($s[$i])) $result .= $s[$i]; // If the desired length is reached if (strlen($result) == $x) { return $result; } } // If total vowels are < X return "-1"; } // Driver code $str = "GeeksForGeeks"; $x = 3; echo firstXvowels($str, $x); // This code is contributed // by princiraj1992 ?>
Javascript
<script> // Javascript implementation of the approach // Function to check if a character is vowel function isVowel(c) { c = (c.toLowerCase()); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function to return first X vowels function firstXvowels(s, x) { // String to store first X vowels var result = ""; for (var i = 0; i < s.length; i++) { // If s[i] is a vowel then // append it to the result if (isVowel(s[i])) result += s[i]; // If the desired length is reached if (result.length == x) { return result; } } // If total vowels are < X return "-1"; } // Driver code var str = "GeeksForGeeks"; var x = 3; document.write( firstXvowels(str, x)); </script>
eeo
Complejidad de tiempo: O(n) Aquí, n es la longitud de la string.