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. public static void main (String[] args) throws java.lang.Exception {
  10. ArrayList<Input>inputs = new ArrayList<Input>();
  11. // inputs.add(new Input("baaaa", "aaaab", true));
  12. inputs.add(new Input("aaaba", "aabaa", true));
  13. inputs.add(new Input("aaaba", "baaaa", true));
  14. // inputs.add(new Input("aaaba", "bbaaa", false));
  15. // inputs.add(new Input("aaaba", "abbaa", false));
  16. // inputs.add(new Input("aaaba", "ababa", false));
  17. // inputs.add(new Input("baaaa", "baaab", false));
  18. // inputs.add(new Input("hello", "elloh", true));
  19. // inputs.add(new Input("waterbottle", "erbottlewat", true));
  20. // inputs.add(new Input("", "", true));
  21. // inputs.add(new Input("hi", "hi", true));
  22. // inputs.add(new Input("hi", "h", false));
  23. // inputs.add(new Input("a", "a", false));
  24. // inputs.add(new Input("hi", "it", false));
  25. // inputs.add(new Input("waterbottle", "erbottlewta", false));
  26. for(int i = 0; i < inputs.size(); i++) {
  27. System.out.printf("input: <%s>\t<%s>\n", inputs.get(i).s1, inputs.get(i).s2);
  28. System.out.printf("\texpected: %b\n", inputs.get(i).expected);
  29. boolean output = isRotation(inputs.get(i).s1, inputs.get(i).s2);
  30. System.out.printf("\toutput: %b\n", output);
  31. if(output != inputs.get(i).expected) {
  32. System.out.printf("\tCASE FAILED!\n");
  33. }
  34. System.out.println();
  35. }
  36. }
  37.  
  38. // check if sub is a substring of str
  39. public static boolean isSubstring(String str, String sub) {
  40. return str.indexOf(sub) > 0;
  41. }
  42.  
  43. // check if s2 is a rotation of sl using only one call to isSubstring
  44. public static boolean isRotation(String s1, String s2) {
  45. int s1Len = s1.length();
  46. int s2Len = s2.length();
  47. if(s1Len != s2Len) {
  48. return false;
  49. }
  50. if(s1 == "" && s2 == "") {
  51. return true;
  52. }
  53. boolean fullMatch;
  54. for(int i = 0; i < s1Len; i++) { // traverse s1 once
  55. fullMatch = true;
  56. for(int j = 0; j < s2Len; j++) {
  57. if(s1.charAt(i) != s2.charAt(j)) {
  58. fullMatch = false;
  59. break;
  60. }
  61. i = (i + 1) % s1Len;
  62. }
  63. if(fullMatch == true) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. static class Input {
  71. String s1;
  72. String s2;
  73. boolean expected;
  74.  
  75. public Input(String s1, String s2, boolean expected) {
  76. this.s1 = s1;
  77. this.s2 = s2;
  78. this.expected = expected;
  79. }
  80. }
  81. }
Time limit exceeded #stdin #stdout 5s 35052KB
stdin
Standard input is empty
stdout
input: <aaaba>	<aabaa>
	expected: true