Dada una string , str que consta de alfabetos ingleses en minúsculas, la tarea es encontrar la cantidad de caracteres únicos presentes en la string.
Ejemplos:
Entrada: str = “geeksforgeeks”
Salida: 7
Explicación: La string dada “geeksforgeeks” contiene 7 caracteres únicos {‘g’, ‘e’, ’k’, ‘s’, ‘f’, ‘o’, ‘r’ }.Entrada: str = «señora»
Salida: 3
Enfoque: El problema dado se puede resolver utilizando la estructura de datos establecida . La idea es inicializar un conjunto desordenado que almacene todos los caracteres distintos de la string dada. El tamaño del conjunto después de atravesar la cuerda es la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Program to count the number of // unique characters in a string int cntDistinct(string str) { // Set to store unique characters // in the given string unordered_set<char> s; // Loop to traverse the string for (int i = 0; i < str.size(); i++) { // Insert current character // into the set s.insert(str[i]); } // Return Answer return s.size(); } // Driver Code int main() { string str = "geeksforgeeks"; cout << cntDistinct(str); return 0; }
Java
// Java program of the above approach import java.util.*; class GFG{ // Program to count the number of // unique characters in a string static int cntDistinct(String str) { // Set to store unique characters // in the given string HashSet<Character> s = new HashSet<Character>(); // Loop to traverse the string for(int i = 0; i < str.length(); i++) { // Insert current character // into the set s.add(str.charAt(i)); } // Return Answer return s.size(); } // Driver Code public static void main(String args[]) { String str = "geeksforgeeks"; System.out.print(cntDistinct(str)); } } // This code is contributed by sanjoy_62
Python3
# Python 3 program of the above approach # Program to count the number of # unique characters in a string def cntDistinct(st): # Set to store unique characters # in the given string s = set([]) # Loop to traverse the string for i in range(len(st)): # Insert current character # into the set s.add(st[i]) # Return Answer return len(s) # Driver Code if __name__ == "__main__": st = "geeksforgeeks" print(cntDistinct(st)) # This code is contributed by ukasp.
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG { // Program to count the number of // unique characters in a string static int cntDistinct(string str) { // Set to store unique characters // in the given string HashSet<char> s = new HashSet<char>(); // Loop to traverse the string for(int i = 0; i < str.Length; i++) { // Insert current character // into the set s.Add(str[i]); } // Return Answer return s.Count; } // Driver Code public static void Main() { string str = "geeksforgeeks"; Console.Write(cntDistinct(str)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Program to count the number of // unique characters in a string function cntDistinct(str) { // Set to store unique characters // in the given string let s = new Set(); // Loop to traverse the string for (let i = 0; i < str.length; i++) { // Insert current character // into the set s.add(str[i]); } // Return Answer return s.size; } // Driver Code let str = "geeksforgeeks"; document.write(cntDistinct(str)); // This code is contributed by Potta Lokesh </script>
7
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por smridhi013btece201 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA