fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Validation
  6. {
  7.  
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. testString("hello");
  11. testString("hell");
  12. testString("lol");
  13.  
  14. testString("heel");
  15. testString("loo");
  16. }
  17.  
  18. public static boolean testString(String testString)
  19. {
  20. String allowedCharacters = "hello";
  21.  
  22. for(int i = 0; i < testString.length(); i++) {
  23. int position = allowedCharacters.indexOf(testString.charAt(i));
  24.  
  25. if(position == -1) {
  26. System.out.println(testString + " - fail");
  27. return false;
  28. } else {
  29. allowedCharacters = allowedCharacters.substring(0, position)
  30. + allowedCharacters.substring(position + 1);
  31. }
  32. }
  33.  
  34.  
  35. System.out.println(testString + " - success");
  36. return true;
  37. }
  38.  
  39. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
hello - success
hell - success
lol - success
heel - fail
loo - fail