fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String consecutive="0123456789abcdefghijklmnopqrstuvwxyz";
  13.  
  14. String queryString="123456789"; // the string that should be checked
  15.  
  16. // temporary String to hold queryString in all lowercase
  17. String tmpString=queryString.toLowerCase();
  18.  
  19. // p1 is the position of the first character of queryString in the consecutive String
  20. int p1=consecutive.indexOf(tmpString.charAt(0));
  21.  
  22. // p2 will be used to hold the position of the following character
  23. int p2=0;
  24.  
  25. // Flag for being consecutive or not
  26. boolean isConsecutive=true;
  27.  
  28. // Loop through the string starting at the second character
  29. for(int i=1;i<tmpString.length();i++) {
  30.  
  31. // Get the position of the character (i) in the consecutive string
  32. p2 = consecutive.indexOf(tmpString.charAt(i));
  33.  
  34. // check if the difference of the 2 positions is +/-1
  35. if(Math.abs(p1-p2)>1) {
  36.  
  37. // if the difference is >1, the characters are not consecutive
  38. // and thus the string is also not consecutive
  39. isConsecutive=false;
  40. break;
  41. }
  42. p1=p2; // Shift the position by one to get to the next character
  43. }
  44.  
  45. // Output the result
  46. if (isConsecutive) {
  47. System.out.println(queryString + " is consecutive");
  48. }
  49. else
  50. {
  51. System.out.println(queryString + " is not consecutive");
  52. }
  53. }
  54. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
123456789 is consecutive