Provjerite slijedi li string redoslijed znakova definiran uzorkom ili ne | Set 2

S obzirom na ulazni niz i uzorak, provjerite slijede li znakovi u ulaznom nizu isti redoslijed koji određuju znakovi prisutni u uzorku. Pretpostavimo da u uzorku neće biti dupliciranih znakova.
Postavljeno je još jedno rješenje za isti problem ovdje .
Primjeri:  
 

Input: string = 'engineers rock' pattern = 'er'; Output: true All 'e' in the input string are before all 'r'. Input: string = 'engineers rock' pattern = 'egr'; Output: false There are two 'e' after 'g' in the input string. Input: string = 'engineers rock' pattern = 'gsr'; Output: false There are one 'r' before 's' in the input string. 


 


Ideja je ovdje smanjiti zadani niz na zadani uzorak. Za znakove navedene u uzorku zadržavamo samo odgovarajuće znakove u nizu. U novom nizu brišemo znakove koji se kontinuirano ponavljaju. Modificirani niz tada treba biti jednak danom uzorku. Na kraju uspoređujemo modificirani niz s danim uzorkom i prema tome vraćamo true ili false.
Ilustracija: 
 

str = 'bfbaeadeacc' pat[] = 'bac' 1) Remove extra characters from str (characters that are not present in pat[] str = 'bbaaacc' [f e and d are removed] 3) Removed consecutive repeating occurrences of characters str = 'bac' 4) Since str is same as pat[] we return true 


Ispod je implementacija gornjih koraka.
 

C++
   // C++ code for the above approach   #include          #include         using     namespace     std  ;   bool     followsPattern  (  string     str       string     pattern  )     {      // Insert all characters of pattern in a hash set      unordered_set   <  char  >     patternSet  ;      for     (  int     i     =     0  ;     i      <     pattern  .  length  ();     i  ++  )     {      patternSet  .  insert  (  pattern  [  i  ]);      }      // Build modified string (string with characters only from pattern are taken)      string     modifiedStr     =     str  ;      for     (  int     i     =     str  .  length  ()     -     1  ;     i     >=     0  ;     i  --  )     {      if     (  patternSet  .  find  (  str  [  i  ])     ==     patternSet  .  end  ())     {      modifiedStr  .  erase  (  i       1  );      }      }      // Remove more than one consecutive occurrences of pattern characters from modified string      for     (  int     i     =     modifiedStr  .  length  ()     -     1  ;     i     >     0  ;     i  --  )     {      if     (  modifiedStr  [  i  ]     ==     modifiedStr  [  i     -     1  ])     {      modifiedStr  .  erase  (  i       1  );      }      }      // After above modifications the length of modified string must be same as pattern length      if     (  pattern  .  length  ()     !=     modifiedStr  .  length  ())     {      return     false  ;      }      // And pattern characters must also be same as modified string characters      for     (  int     i     =     0  ;     i      <     pattern  .  length  ();     i  ++  )     {      if     (  pattern  [  i  ]     !=     modifiedStr  [  i  ])     {      return     false  ;      }      }      return     true  ;   }   int     main  ()     {      string     str     =     'engineers rock'  ;      string     pattern     =     'er'  ;      cout      < <     'Expected: true Actual: '      < <     followsPattern  (  str       pattern  )      < <     endl  ;      str     =     'engineers rock'  ;      pattern     =     'egr'  ;      cout      < <     'Expected: false Actual: '      < <     followsPattern  (  str       pattern  )      < <     endl  ;      str     =     'engineers rock'  ;      pattern     =     'gsr'  ;      cout      < <     'Expected: false Actual: '      < <     followsPattern  (  str       pattern  )      < <     endl  ;      str     =     'engineers rock'  ;      pattern     =     'eger'  ;      cout      < <     'Expected: true Actual: '      < <     followsPattern  (  str       pattern  )      < <     endl  ;      return     0  ;   }   // This code is contributed by adityashatmfh   
Java
   // Java program to check if characters of a string follow   // pattern defined by given pattern.   import     java.util.*  ;   public     class   OrderOfCharactersForPattern   {      public     static     boolean     followsPattern  (  String     str       String     pattern  )      {      // Insert all characters of pattern in a hash set      Set   <  Character  >     patternSet     =     neHashSet   <>  ();      for     (  int     i  =  0  ;     i   <  pattern  .  length  ();     i  ++  )      patternSet  .  add  (  pattern  .  charAt  (  i  ));      // Build modified string (string with characters only from      // pattern are taken)      StringBuilder     modifiedString     =     new     StringBuilder  (  str  );      for     (  int     i  =  str  .  length  ()  -  1  ;     i  >=  0  ;     i  --  )      if     (  !  patternSet  .  contains  (  modifiedString  .  charAt  (  i  )))      modifiedString  .  deleteCharAt  (  i  );      // Remove more than one consecutive occurrences of pattern      // characters from modified string.      for     (  int     i  =  modifiedString  .  length  ()  -  1  ;     i  >  0  ;     i  --  )      if     (  modifiedString  .  charAt  (  i  )     ==     modifiedString  .  charAt  (  i  -  1  ))      modifiedString  .  deleteCharAt  (  i  );      // After above modifications the length of modified string      // must be same as pattern length      if     (  pattern  .  length  ()     !=     modifiedString  .  length  ())      return     false  ;      // And pattern characters must also be same as modified string      // characters      for     (  int     i  =  0  ;     i   <  pattern  .  length  ();     i  ++  )      if     (  pattern  .  charAt  (  i  )     !=     modifiedString  .  charAt  (  i  ))      return     false  ;      return     true  ;      }      // Driver program      int     main  ()      {      String     str     =     'engineers rock'  ;      String     pattern     =     'er'  ;      System  .  out  .  println  (  'Expected: true Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'egr'  ;      System  .  out  .  println  (  'Expected: false Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'gsr'  ;      System  .  out  .  println  (  'Expected: false Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'eger'  ;      System  .  out  .  println  (  'Expected: true Actual: '     +      followsPattern  (  str       pattern  ));      return     0  ;      }   }   
Python3
   # Python3 program to check if characters of    # a string follow pattern defined by given pattern.   def   followsPattern  (  string     pattern  ):   # Insert all characters of pattern in a hash set   patternSet   =   set  ()   for   i   in   range  (  len  (  pattern  )):   patternSet  .  add  (  pattern  [  i  ])   # Build modified string (string with characters    # only from pattern are taken)   modifiedString   =   string   for   i   in   range  (  len  (  string  )   -   1     -  1     -  1  ):   if   not   modifiedString  [  i  ]   in   patternSet  :   modifiedString   =   modifiedString  [:  i  ]   +    modifiedString  [  i   +   1  :]   # Remove more than one consecutive occurrences    # of pattern characters from modified string.   for   i   in   range  (  len  (  modifiedString  )   -   1     0     -  1  ):   if   modifiedString  [  i  ]   ==   modifiedString  [  i   -   1  ]:   modifiedString   =   modifiedString  [:  i  ]   +    modifiedString  [  i   +   1  :]   # After above modifications the length of    # modified string must be same as pattern length   if   len  (  pattern  )   !=   len  (  modifiedString  ):   return   False   # And pattern characters must also be same    # as modified string characters   for   i   in   range  (  len  (  pattern  )):   if   pattern  [  i  ]   !=   modifiedString  [  i  ]:   return   False   return   True   # Driver Code   if   __name__   ==   '__main__'  :   string   =   'engineers rock'   pattern   =   'er'   print  (  'Expected: true Actual:'     followsPattern  (  string     pattern  ))   string   =   'engineers rock'   pattern   =   'egr'   print  (  'Expected: false Actual:'     followsPattern  (  string     pattern  ))   string   =   'engineers rock'   pattern   =   'gsr'   print  (  'Expected: false Actual:'     followsPattern  (  string     pattern  ))   string   =   'engineers rock'   pattern   =   'eger'   print  (  'Expected: true Actual:'     followsPattern  (  string     pattern  ))   # This code is contributed by   # sanjeev2552   
C#
   // C# program to check if characters of a string follow   // pattern defined by given pattern.   using     System  ;   using     System.Collections.Generic  ;   using     System.Text  ;   class     GFG   {      public     static     bool     followsPattern  (  String     str       String     pattern  )      {      // Insert all characters of pattern in a hash set      HashSet   <  char  >     patternSet     =     new     HashSet   <  char  >  ();      for     (  int     i     =     0  ;     i      <     pattern  .  Length  ;     i  ++  )      patternSet  .  Add  (  pattern  [  i  ]);      // Build modified string (string with characters       // only from pattern are taken)      StringBuilder     modifiedString     =     new     StringBuilder  (  str  );      for     (  int     i     =     str  .  Length     -     1  ;     i     >=     0  ;     i  --  )      if     (  !  patternSet  .  Contains  (  modifiedString  [  i  ]))      modifiedString  .  Remove  (  i       1  );      // Remove more than one consecutive occurrences of pattern      // characters from modified string.      for     (  int     i     =     modifiedString  .  Length     -     1  ;     i     >     0  ;     i  --  )      if     (  modifiedString  [  i  ]     ==     modifiedString  [  i     -     1  ])      modifiedString  .  Remove  (  i       1  );      // After above modifications the length of modified string      // must be same as pattern length      if     (  pattern  .  Length     !=     modifiedString  .  Length  )      return     false  ;      // And pattern characters must also be same       // as modified string characters      for     (  int     i     =     0  ;     i      <     pattern  .  Length  ;     i  ++  )      if     (  pattern  [  i  ]     !=     modifiedString  [  i  ])      return     false  ;      return     true  ;      }      // Driver program      public     static     void     Main  (  String  []     args  )      {      String     str     =     'engineers rock'  ;      String     pattern     =     'er'  ;      Console  .  WriteLine  (  'Expected: true Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'egr'  ;      Console  .  WriteLine  (  'Expected: false Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'gsr'  ;      Console  .  WriteLine  (  'Expected: false Actual: '     +      followsPattern  (  str       pattern  ));      str     =     'engineers rock'  ;      pattern     =     'eger'  ;      Console  .  WriteLine  (  'Expected: true Actual: '     +      followsPattern  (  str       pattern  ));      }   }   // This code is contributed by 29AjayKumar   
JavaScript
    <  script  >   // Javascript program to check if characters of a string follow   // pattern defined by given pattern.   function     followsPattern  (  str       pattern  )   {      // Insert all characters of pattern in a hash set      let     patternSet     =     new     Set  ();      for     (  let     i  =  0  ;     i   <  pattern  .  length  ;     i  ++  )      patternSet  .  add  (  pattern  [  i  ]);          // Build modified string (string with characters only from      // pattern are taken)      let     modifiedString     =     (  str  ).  split  (  ''  );      for     (  let     i  =  str  .  length  -  1  ;     i  >=  0  ;     i  --  )      if     (  !  patternSet  .  has  (  modifiedString  [  i  ]))      modifiedString  .  splice  (  i    1  );          // Remove more than one consecutive occurrences of pattern      // characters from modified string.      for     (  let     i  =  modifiedString  .  length  -  1  ;     i  >  0  ;     i  --  )      if     (  modifiedString  [  i  ]     ==     modifiedString  [  i  -  1  ])      modifiedString  .  splice  (  i    1  );          // After above modifications the length of modified string      // must be same as pattern length      if     (  pattern  .  length     !=     modifiedString  .  length  )      return     false  ;          // And pattern characters must also be same as modified string      // characters      for     (  let     i  =  0  ;     i   <  pattern  .  length  ;     i  ++  )      if     (  pattern  [  i  ]     !=     modifiedString  [  i  ])      return     false  ;          return     true  ;   }   // Driver program   let     str     =     'engineers rock'  ;   let     pattern     =     'er'  ;   document  .  write  (  'Expected: true Actual: '     +      followsPattern  (  str       pattern  )  +  '  
'
); str = 'engineers rock' ; pattern = 'egr' ; document . write ( 'Expected: false Actual: ' + followsPattern ( str pattern ) + '
'
); str = 'engineers rock' ; pattern = 'gsr' ; document . write ( 'Expected: false Actual: ' + followsPattern ( str pattern ) + '
'
); str = 'engineers rock' ; pattern = 'eger' ; document . write ( 'Expected: true Actual: ' + followsPattern ( str pattern ) + '
'
); // This code is contributed by rag2127 < /script>

Izlaz:  
 

Expected: true Actual: true Expected: false Actual: false Expected: false Actual: false Expected: true Actual: true 


Vremenska složenost: Vremenska složenost gornjih implementacija zapravo je O(mn + n^2) jer koristimo deleteCharAt() za uklanjanje znakova. Gornje rješenje možemo optimizirati za rad u linearnom vremenu. Umjesto korištenja deleteCharAr() možemo stvoriti prazan niz i dodati mu samo potrebne znakove.
StringBuilder se koristi za rad na ulaznom nizu. To je zato što je StringBuilder promjenjiv dok je String nepromjenjivi objekt. Za stvaranje novog niza potrebno je O(n) prostora tako da je dodatni prostor O(n).
Raspravljali smo o još dva pristupa rješavanju ovog problema. 
Provjerite slijedi li string redoslijed znakova definiran uzorkom ili ne | Set 1  
Provjerite slijedi li string redoslijed znakova definiran uzorkom ili ne | Set 3