Guest User

Untitled

a guest
Jan 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. public class d3 {
  2. public static void main(String[] args){
  3. String Search="loop";
  4. String[] letters = Search.split("") ;
  5. int counter;
  6.  
  7. String[] words={"pole","pool","lopo","book","kobo"};
  8.  
  9. for(int i=0;i<words.length;i++)
  10. {
  11. counter=0;
  12. String ssplit[] = words[i].split("");
  13. for(int j=0;j<words[i].length();j++)
  14. {
  15.  
  16. if(letters.length==ssplit.length)
  17. {
  18. for(int k=0;k<letters.length;k++)
  19. {
  20. if(letters[j].equals(ssplit[k]))
  21. {counter++;
  22. ssplit[k]="*";
  23. break;
  24.  
  25.  
  26. }
  27.  
  28.  
  29.  
  30. }
  31. if (counter == 4)
  32. {
  33. System.out.println(words[i]);
  34.  
  35. }
  36.  
  37. }
  38. }
  39.  
  40.  
  41. }
  42.  
  43. }
  44. }
  45.  
  46. public class d3 {
  47. public static void main(String[] args){
  48. String Search="loop";
  49. String[] letters = Search.split("") ;
  50. int counter;
  51.  
  52. String[] words={"pole","pool","lopo","book","kobo"};
  53.  
  54. for(int i=0;i<words.length;i++)
  55. {
  56. counter=0;
  57. String ssplit[] = words[i].split("");
  58. for(int j=0;j<words[i].length();j++)
  59. {
  60. if(letters.length==ssplit.length)
  61. {
  62. for(int k=0;k<letters.length;k++)
  63. {
  64. if(letters[j].equals(ssplit[k]))
  65. {
  66. counter++;
  67. ssplit[k]="*";
  68. break;
  69. }
  70. }
  71. if (counter == 4)
  72. {
  73. System.out.println(words[i]);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
  81. for(int j=0;j<words[i].length();j++)
  82. {
  83. if(letters.length==ssplit.length)
  84. {
  85. ...
  86. }
  87. }
  88.  
  89. if(letters.length==ssplit.length)
  90. {
  91. for(int j=0;j<ssplit.length;j++)
  92. {
  93. ...
  94. }
  95. }
  96.  
  97. if(letters.length!=ssplit.length)
  98. {
  99. continue;
  100. }
  101.  
  102. for(int j=0;j<ssplit.length;j++)
  103. {
  104. ...
  105. }
  106.  
  107. if (counter == 4)
  108. {
  109. System.out.println(words[i]);
  110. }
  111.  
  112. for(int j=0;j<words[i].length();j++)
  113. {
  114. for(int k=0;k<letters.length;k++)
  115. {
  116. if(letters[j].equals(ssplit[k]))
  117. {
  118. counter++;
  119. ssplit[k]="*";
  120. break;
  121. }
  122. }
  123. }
  124.  
  125. class WordFinder {
  126. private final String[] words;
  127. WordFinder(String[] words) {
  128. this.words = new String[words.length];
  129. System.arraycopy(words, 0, this.words, 0, words.length);
  130. }
  131. String[] find(String word) {
  132. // Your code here
  133. }
  134. }
  135.  
  136. String[] find(String word) {
  137. String[] result = new String[this.words.length];
  138. char[] letters = word.toCharArray();
  139. int counter = 0;
  140. for (String candidate : this.words) {
  141. if ( this.matches(letters, candidate.toCharArray()) ) {
  142. result[counter++] = candidate;
  143. }
  144. }
  145. return Arrays.copyOf(result, counter);
  146. }
  147.  
  148. public class WordFinder {
  149.  
  150. private final String[] words;
  151.  
  152. public WordFinder(String[] words) {
  153. this.words = new String[words.length];
  154. System.arraycopy(words, 0, this.words, 0, words.length);
  155. }
  156.  
  157. public String[] find(String word) {
  158. String[] result = new String[this.words.length];
  159. char[] letters = word.toCharArray();
  160. int counter = 0;
  161. for (String candidate : this.words) {
  162. if ( matches(letters, candidate.toCharArray()) ) {
  163. result[counter++] = candidate;
  164. }
  165. }
  166. return Arrays.copyOf(result, counter);
  167. }
  168.  
  169. private boolean matches(char[] requirement, char[] candidate) {
  170. /* Edited to replace Arrays.hashCode with Arrays.equals, see comments
  171. return candidate.length==requirement.length &&
  172. hash(candidate)==hash(requirement);*/
  173.  
  174. if ( candidate.length!=requirement.length ) {
  175. return false;
  176. }
  177. char left = Arrays.copyOf(requirement, requirement.length);
  178. Arrays.sort(left);
  179. char right = Arrays.copyOf(candidate, candidate.length);
  180. Arrays.sort(right );
  181. return Arrays.equals(left, right);
  182. }
  183.  
  184. /*private int hash(char[] array) {
  185. char[] copy = Arrays.copyOf(array, array.length);
  186. Arrays.sort(copy);
  187. return Arrays.hashCode(copy);
  188. }*/
  189. }
Add Comment
Please, Sign In to add comment