×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Nazmus Shakib
Added: Mar 15, 2021 1:52 PM
Views: 4775
Tags: no tags
  1.  
  2. public class checkCode {
  3.     static  boolean  check(String path) {
  4.        
  5.         // first character of the path should always be from (F,B,L,R)
  6.         if(path.charAt(0)!='F' && path.charAt(0)!='B' && path.charAt(0)!='L' && path.charAt(0)!='R' ) {
  7.                 return false;
  8.         }
  9.        
  10.         // declare steps to count number of steps in a direction
  11.         // steps should not be greater than 99
  12.         int steps=0;
  13.        
  14.         //iterate over the string to check its validity
  15.         for(int i=1;i<path.length();i++) {
  16.                 char c = path.charAt(i);
  17.                
  18.                 // the current letter is from (F,B,L,R) or not
  19.                 if(c=='F' || c=='B' || c=='L' || c=='R') {
  20.                         // steps == 0 means last letter was also from (F,B,L,R)
  21.                         // and no two letter from (F,B,L,R) can appear simultaneously
  22.                         // steps > 99, but number of steps should be in range [1,99]
  23.                         if(steps==0 || steps>99) return false;
  24.                         else steps=0;
  25.                 }
  26.                 // c is some number from 0 to 9
  27.                 else if(c>=0 && c<=9) {
  28.                         steps += (c-'0');
  29.                 }
  30.                 // c is neither a number nor a letter from (F,B,L,R)
  31.                 else {
  32.                         return false;
  33.                 }
  34.         }
  35.        
  36.         // number of steps for the last move is valid or not
  37.         if(steps==0 || steps>99) return false;
  38.        
  39.         // every condition is passed hence true
  40.         return true;
  41. }
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.