Niz z največjim številom edinstvenih znakov

Glede na niz nizov je naloga natisniti niz z največje število edinstvenih likov.

Opomba:

  • Nizi so sestavljeni iz malih črk.
  • Če obstaja več nizov, natisnite katerega koli od njih.

Primeri:  

Vnos: arr[] = ['abc' 'geeksforgeeks' 'gfg' 'code']
Izhod: "geeki za geeke" 
Pojasnilo: 'abc' ima 3 edinstvene znake 'geeksforgeeks' ima 7 edinstvenih znakov
'gfg' ima 3 edinstvene znake 'code' ima 4 edinstvene znake.

Vnos: arr[] = ['zdravo' 'svet' 'programiranje' 'zebra']
Izhod: 'programiranje' 
Pojasnilo: 'programiranje' ima 8 edinstvenih znakov, kar je največ.

Pristop:

Ideja je sistematično preučiti vsak niz in slediti, katere male črke se v njem pojavijo, z uporabo logične matrike, ki šteje te edinstvene znake in spremljati, kateri niz je doslej dal največje število.

Pristop korak za korakom:

  1. Ponavljajte vsak niz v zbirki.
  2. Za vsak niz uporabite logično polje, da označite, katere črke so prisotne.
  3. Preštejte, koliko različnih črk je bilo označenih kot prisotne.
  4. Če to število preseže prejšnje največje število, posodobite največje število in si zapomnite položaj tega niza.
  5. Vrne niz z največjim številom najdenih edinstvenih znakov.
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  ));   

Izhod
geeksforgeeks 

Časovna zapletenost: O(n*m) kjer n je velikost dane matrike nizov in m je največja velikost niza v dani matriki.
Pomožni prostor: O(1)