📌  相关文章
📜  具有最大唯一字符数的字符串

📅  最后修改于: 2022-05-13 01:57:08.547000             🧑  作者: Mango

具有最大唯一字符数的字符串

给定一个字符串列表,找出其中最大的字符串。最大字符串是唯一字符数最多的字符串。

例子:

Input : "AN KOW", "LO JO", "ZEW DO RO" 
Output : "ZEW DO RO" 
Explanation : 
"ZEW DO RO" has maximum distinct letters.

Input : "ROMEO", "EMINEM", "RADO"
Output : "ROMEO" 
Explanation : In case of tie, we can print
any of the strings.

我们遍历字符串并使用一个布尔数组来检查字母的存在。此外,跟踪最大唯一字母。返回具有最大不同字符数的字符串。

C++
// C++ code to find
// the largest string
#include 
using namespace std;
 
// Function to find string
// with maximum number of
// unique characters.
void LargestString(string *na)
{
    int N = sizeof(na) /
            sizeof(na[0]);
    int c[N];
 
    // Index of string with
    // maximum unique characters
    int m = 1;
 
    // iterate through
    // all strings
    for (int j = 0; j < N; j++)
    {
        // array indicating any
        // alphabet included or
        // not included
        bool character[26];
 
        // 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)
            {
                c[j]++;
                character[x] = true;
            }
        }
 
        // keep track of maximum
        // number of alphabets
        if (c[j] > c[m])
            m = j;
    }
    // print result
    cout << na[m] << endl;
}
 
// Driver code
int main()
{
    string na[] = {"BOB", "A AB C JOHNSON",
                   "ANJALI","ASKRIT",
                   "ARMAN MALLIK"};
 
    LargestString(na);
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Java
// Java code to find the largest string
import java.lang.*;
import java.io.*;
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];
 
        // Index of string with maximum unique
        // characters
        int m = 0;
 
        // iterate through all strings
        for (int j = 0; j < N; j++)
        {
            // array indicating any alphabet
            // included or not included
            boolean character[] = new boolean[26];
 
            // 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))
                {
                    c[j]++;
                    character[x] = true;
                }
            }
 
            // keep track of maximum number of alphabets
            if (c[j] > c[m])
                m = j;
        }
 
        // print result
        System.out.println(na[m]);
    }
 
    // 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
     
    # Index of string with
    # maximum unique characters
    m = 0
     
    # Iterate through all strings
    for j in range(N):
         
        # Array indicating any alphabet
        # included or not included
        character = [False] * 26
         
        # 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)):
                c[j] += 1
                character[x] = True
             
            # keep track of maximum
            # number of alphabets
            if (c[j] > c[m]):
                m = j
                 
    # print result
    print(na[m])
 
# Driver code
na = ["BOB", "A AB C JOHNSON","ANJALI",
                "ASKRIT", "ARMAN MALLIK"]
LargestString(na)
 
# This article is Contributed by Sharad Bhardwaj.


C#
// C# code to find the largest string
using System;
 
class GFG {
 
    // 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];
 
        // Index of string with maximum unique
        // characters
        int m = 0;
 
        // iterate through all strings
        for (int j = 0; j < N; j++)
        {
            // array indicating any alphabet
            // included or not included
            bool []character = new bool[26];
 
            // count number of unique alphabets
            // in each string
            for (int k = 0; k < na[j].Length; k++)
            {
                int x = na[j][k] - 'A';
                 
                if ((na[j][k] != ' ') &&
                        (character[x] == false))
                {
                    c[j]++;
                    character[x] = true;
                }
            }
 
            // keep track of maximum number of
            // alphabets
            if (c[j] > c[m])
                m = j;
        }
 
        // print result
        Console.Write(na[m]);
    }
 
    // Driver code
    public static void Main()
    {
        string []na = {"BOB", "A AB C JOHNSON",
           "ANJALI", "ASKRIT", "ARMAN MALLIK"};
 
        LargestString(na);
    }
}
 
// This code is contributed by nitin mittal.


PHP
 $c[$m])
            $m = $j;
    }
 
    // print result
    echo $na[$m]."\n";
}
 
// Driver code
$na = array("BOB", "A AB C JOHNSON",
            "ASKRIT", "ARMAN MALLIK",
                           "ANJALI");
LargestString($na);
 
// This code is contributed by mits
?>


Javascript


输出:

A AB C JOHNSON