Streng med maksimalt antal unikke tegn

Givet en række strenge er opgaven at udskrive strengen med maksimalt antal af unikke karakterer.

Note:

  • Strings består af små bogstaver.
  • Hvis der findes flere strenge, skal du udskrive en af ​​dem.

Eksempler:  

Input: arr[] = ['abc' 'geeksforgeeks' 'gfg' 'kode']
Produktion: "nørder for nørder" 
Forklaring: 'abc' har 3 unikke karakterer 'geeksforgeeks' har 7 unikke karakterer
'gfg' har 3 unikke tegn 'code' har 4 unikke tegn.

Input: arr[] = ['hej' 'verden' 'programmering' 'zebra']
Produktion: 'programmering' 
Forklaring: 'programmering' har 8 unikke tegn, hvilket er det maksimale.

Nærme sig:

Ideen er systematisk at undersøge hver streng, der sporer, hvilke små bogstaver der forekommer i den ved hjælp af en boolsk matrix, der tæller de unikke tegn og holder styr på, hvilken streng der har givet det højeste antal hidtil.

Trin for trin tilgang:

  1. Gentag gennem hver streng i samlingen.
  2. For hver streng skal du bruge et boolesk array til at markere, hvilke bogstaver der er til stede.
  3. Tæl hvor mange forskellige bogstaver der blev markeret som tilstede.
  4. Hvis dette antal overstiger det tidligere maksimum, opdater maksimum og husk denne strengs position.
  5. Returner strengen med det højeste antal unikke tegn fundet.
C++
   // C++ code to find string with maximum   // number of unique characters.   #include          using     namespace     std  ;   // Function to find string with    // maximum number of unique characters.   string     maxString  (  vector   <  string  >     &  arr  )     {      int     n     =     arr  .  size  ();          int     index     =     -1       maxCnt     =     0  ;          for     (  int     i  =  0  ;     i   <  n  ;     i  ++  )     {          vector   <  bool  >     map  (  26       false  );          for     (  int     j  =  0  ;     j   <  arr  [  i  ].  length  ();     j  ++  )     {      map  [  arr  [  i  ][  j  ]     -     'a'  ]     =     true  ;      }          // Find number of unique char       int     cnt     =     0  ;      for     (  int     j  =  0  ;     j   <  26  ;     j  ++  )     {      if     (  map  [  j  ]     ==     true  )     cnt  ++  ;      }          // If unique count is greater      if     (  cnt     >     maxCnt  )     {      maxCnt     =     cnt  ;      index     =     i  ;      }      }          return     arr  [  index  ];   }   int     main  ()     {      vector   <  string  >     arr     =     {  'abc'       'geeksforgeeks'       'gfg'       'code'  };      cout      < <     maxString  (  arr  );      return     0  ;   }   
Java
   // Java code to find string with maximum   // number of unique characters.   import     java.util.*  ;   class   GfG     {      // Function to find string with       // maximum number of unique characters.      static     String     maxString  (  String  []     arr  )     {      int     n     =     arr  .  length  ;      int     index     =     -  1       maxCnt     =     0  ;      for     (  int     i     =     0  ;     i      <     n  ;     i  ++  )     {      boolean  []     map     =     new     boolean  [  26  ]  ;      for     (  int     j     =     0  ;     j      <     arr  [  i  ]  .  length  ();     j  ++  )     {      map  [  arr  [  i  ]  .  charAt  (  j  )     -     'a'  ]     =     true  ;      }      // Find number of unique char       int     cnt     =     0  ;      for     (  int     j     =     0  ;     j      <     26  ;     j  ++  )     {      if     (  map  [  j  ]     ==     true  )     cnt  ++  ;      }      // If unique count is greater      if     (  cnt     >     maxCnt  )     {      maxCnt     =     cnt  ;      index     =     i  ;      }      }      return     arr  [  index  ]  ;      }      public     static     void     main  (  String  []     args  )     {      String  []     arr     =     {  'abc'       'geeksforgeeks'       'gfg'       'code'  };      System  .  out  .  println  (  maxString  (  arr  ));      }   }   
Python
   # Python code to find string with maximum   # number of unique characters.   # Function to find string with    # maximum number of unique characters.   def   maxString  (  arr  ):   n   =   len  (  arr  )   index   =   -  1   maxCnt   =   0   for   i   in   range  (  n  ):   map   =   [  False  ]   *   26   for   j   in   range  (  len  (  arr  [  i  ])):   map  [  ord  (  arr  [  i  ][  j  ])   -   ord  (  'a'  )]   =   True   # Find number of unique char    cnt   =   sum  (  1   for   j   in   range  (  26  )   if   map  [  j  ])   # If unique count is greater   if   cnt   >   maxCnt  :   maxCnt   =   cnt   index   =   i   return   arr  [  index  ]   if   __name__   ==   '__main__'  :   arr   =   [  'abc'     'geeksforgeeks'     'gfg'     'code'  ]   print  (  maxString  (  arr  ))   
C#
   // C# code to find string with maximum   // number of unique characters.   using     System  ;   class     GfG     {      // Function to find string with       // maximum number of unique characters.      static     string     maxString  (  string  []     arr  )     {      int     n     =     arr  .  Length  ;      int     index     =     -  1       maxCnt     =     0  ;      for     (  int     i     =     0  ;     i      <     n  ;     i  ++  )     {      bool  []     map     =     new     bool  [  26  ];      for     (  int     j     =     0  ;     j      <     arr  [  i  ].  Length  ;     j  ++  )     {      map  [  arr  [  i  ][  j  ]     -     'a'  ]     =     true  ;      }      // Find number of unique char       int     cnt     =     0  ;      for     (  int     j     =     0  ;     j      <     26  ;     j  ++  )     {      if     (  map  [  j  ]     ==     true  )     cnt  ++  ;      }      // If unique count is greater      if     (  cnt     >     maxCnt  )     {      maxCnt     =     cnt  ;      index     =     i  ;      }      }      return     arr  [  index  ];      }      static     void     Main  ()     {      string  []     arr     =     {  'abc'       'geeksforgeeks'       'gfg'       'code'  };      Console  .  WriteLine  (  maxString  (  arr  ));      }   }   
JavaScript
   // JavaScript code to find string with maximum   // number of unique characters.   // Function to find string with    // maximum number of unique characters.   function     maxString  (  arr  )     {      let     n     =     arr  .  length  ;      let     index     =     -  1       maxCnt     =     0  ;      for     (  let     i     =     0  ;     i      <     n  ;     i  ++  )     {      let     map     =     new     Array  (  26  ).  fill  (  false  );      for     (  let     j     =     0  ;     j      <     arr  [  i  ].  length  ;     j  ++  )     {      map  [  arr  [  i  ].  charCodeAt  (  j  )     -     'a'  .  charCodeAt  (  0  )]     =     true  ;      }      // Find number of unique char       let     cnt     =     0  ;      for     (  let     j     =     0  ;     j      <     26  ;     j  ++  )     {      if     (  map  [  j  ]     ===     true  )     cnt  ++  ;      }      // If unique count is greater      if     (  cnt     >     maxCnt  )     {      maxCnt     =     cnt  ;      index     =     i  ;      }      }      return     arr  [  index  ];   }   let     arr     =     [  'abc'       'geeksforgeeks'       'gfg'       'code'  ];   console  .  log  (  maxString  (  arr  ));   

Produktion
geeksforgeeks 

Tidskompleksitet: O(n*m) hvor n er størrelsen af ​​den givne strengmatrix og m er den største størrelse af strengen til stede i det givne array.
Hjælpeplads: O(1)