Tekenreeks met maximaal aantal unieke tekens

Gegeven een array van strings is het de taak om de string af te drukken met de maximaal aantal van unieke karakters.

Opmerking:

  • Tekenreeksen bestaan ​​uit kleine letters.
  • Als er meerdere strings bestaan, druk dan één ervan af.

Voorbeelden:  

Invoer: arr[] = ['abc' 'geeksforgeeks' 'gfg' 'code']
Uitgang: "nerds voor nerds" 
Uitleg: 'abc' heeft 3 unieke karakters 'geeksforgeeks' heeft 7 unieke karakters
'gfg' heeft 3 unieke karakters. 'code' heeft 4 unieke karakters.

Invoer: arr[] = ['hallo' 'wereld' 'programmeren' 'zebra']
Uitgang: 'programmeren' 
Uitleg: 'programmeren' heeft 8 unieke karakters, wat het maximum is.

Benadering:

Het idee is om elke string systematisch te onderzoeken en bij te houden welke kleine letters erin voorkomen, met behulp van een booleaanse array die de unieke tekens telt en bijhoudt welke string tot nu toe het hoogste aantal heeft opgeleverd.

Stap voor stap aanpak:

  1. Herhaal elke tekenreeks in de verzameling.
  2. Gebruik voor elke string een Booleaanse array om aan te geven welke letters aanwezig zijn.
  3. Tel hoeveel verschillende letters als aanwezig zijn gemarkeerd.
  4. Als deze telling het vorige maximum overschrijdt, update dan het maximum en onthoud de positie van deze string.
  5. Retourneert de tekenreeks met het hoogste aantal gevonden unieke tekens.
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  ));   

Uitvoer
geeksforgeeks 

Tijdcomplexiteit: O(n*m) waar N is de grootte van de gegeven stringarray en M is de grootste grootte van de string die aanwezig is in de gegeven array.
Hulpruimte: O(1)