String con el número máximo de caracteres únicos

Dada una lista de strings Str , encuentre la string más grande entre todas. La string más grande es la string con el mayor número de caracteres únicos.

Ejemplo: 

C++

// C++ code to find
// the largest string
#include <bits/stdc++.h>
using namespace std;
 
// Function to find string
// with maximum number of
// unique characters.
void LargestString(string* na, int N)
{
    // To keep track of maximum unqiue character
    // string
    int maxx = 0;
    string result;
 
    // iterate through
    // all strings
    for (int j = 0; j < N; j++) {
        // array indicating any
        // alphabet included or
        // not included
        bool character[26];
 
        // To count the unique character
        int count = 0;
 
        // count number of unique
        // alphabets in each string
        for (int k = 0; k < na[j].size(); k++) {
            int x = (int)(na[j][k] - 'A');
            if (na[j][k] != ' ' && character[x] == false) {
                count++;
                character[x] = true;
            }
        }
 
        // keep track of maximum
        // number of alphabets
        if(count > maxx){
            result = na[j];
            maxx = count;
        }
         
    }
   
    // print result
    cout << result << endl;
}
 
// Driver code
int main()
{
    string na[] = { "BOB", "A AB C JOHNSON", "ANJALI",
                    "ASKRIT", "ARMAN MALLIK" };
    int N = sizeof(na) / sizeof(na[0]);
 
    LargestString(na, N);
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

Java

// Java code to find the largest string
import java.io.*;
import java.lang.*;
import java.util.Arrays;
 
class Geek {
 
    // Function to find string with maximum
    // number of unique characters.
    public static void LargestString(String na[])
    {
        int N = na.length;
        int c[] = new int[N];
 
        // To keep track of maximum unqiue character
        // string
        int maxx = 0;
        String result = "";
 
        // iterate through all strings
        for (int j = 0; j < N; j++) {
            // array indicating any alphabet
            // included or not included
            boolean character[] = new boolean[26];
 
            // To count the unique character
            int count = 0;
 
            // count number of unique alphabets in each
            // string
            for (int k = 0; k < na[j].length(); k++) {
                int x = na[j].charAt(k) - 'A';
                if ((na[j].charAt(k) != ' ')
                    && (character[x] == false)) {
                    count++;
                    character[x] = true;
                }
            }
 
            // keep track of maximum number of alphabets
            if (count > maxx) {
                result = na[j];
                maxx = count;
            }
        }
 
        // print result
        System.out.println(result);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String na[] = { "BOB", "A AB C JOHNSON", "ANJALI",
                        "ASKRIT", "ARMAN MALLIK" };
 
        LargestString(na);
    }
}

Python3

# Python3 code to find the largest string
 
# Function to find string with maximum
# number of unique characters.
 
 
def LargestString(na):
    N = len(na)
    c = [0] * N
 
    # To keep track of maximum unqiue character
    # string
    maxx = 0
    result = ""
 
    # Iterate through all strings
    for j in range(N):
 
        # Array indicating any alphabet
        # included or not included
        character = [False] * 26
 
        # To count the unique character
        count = 0
 
        # count number of unique
        # alphabets in each string
        for k in range(len(na[j])):
            x = ord(na[j][k]) - ord('A')
 
            if ((na[j][k] != ' ') and (character[x] == False)):
                count += 1
                character[x] = True
 
            # keep track of maximum
                # number of alphabets
            if(count > maxx):
                result = na[j]
                maxx = count
 
    # print result
    print(result)
 
 
# Driver code
na = ["BOB", "A AB C JOHNSON", "ANJALI",
      "ASKRIT", "ARMAN MALLIK"]
LargestString(na)
 
# This article is Contributed by Sharad Bhardwaj.

Publicación traducida automáticamente

Artículo escrito por PriyankaKhatri y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *