Skip to content

Commit bb50be2

Browse files
committed
can arrange word
1 parent 23cabc4 commit bb50be2

2 files changed

Lines changed: 72 additions & 51 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.inuker.solution;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* Created by dingjikerbo on 2017/9/29.
7+
*/
8+
9+
/**
10+
* 单词接龙,前一个单词的尾巴和后一个单词的头相同就算接上了
11+
* 如果要返回接龙的结果,可以给HashSet改成List
12+
* 如果要返回所有可能的结果呢?
13+
*/
14+
public class CanArrangeWords {
15+
16+
public static boolean canArrangeWords(String[] words) {
17+
HashSet<String> set = new HashSet<String>();
18+
return helper(null, words, set);
19+
}
20+
21+
public static boolean helper(String word, String[] words, HashSet<String> set) {
22+
if (set.size() == words.length) {
23+
return true;
24+
}
25+
26+
for (String s : words) {
27+
if (!set.contains(s) && (word == null || s.charAt(0) == word.charAt(word.length() - 1))) {
28+
set.add(s);
29+
if (helper(s, words, set)) {
30+
return true;
31+
}
32+
set.remove(s);
33+
}
34+
}
35+
return false;
36+
}
37+
}
Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,56 @@
11
package com.example;
22

3-
4-
import com.inuker.solution.BinaryTreePreorderTraversal;
5-
import com.inuker.solution.PathSumIII;
6-
import com.leetcode.library.TreeNode;
7-
8-
import java.sql.Timestamp;
9-
import java.util.Calendar;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
106
import java.util.LinkedList;
117
import java.util.List;
128

139
public class Main {
1410

1511
public static void main(String[] args) {
16-
TreeNode nodef32 = new TreeNode(-3);
17-
TreeNode node32 = new TreeNode(3, nodef32, null);
18-
TreeNode nodef2 = new TreeNode(-2);
19-
TreeNode node1 = new TreeNode(1);
20-
TreeNode node11 = new TreeNode(11);
21-
22-
TreeNode node3 = new TreeNode(3, node32, nodef2);
23-
TreeNode node2 = new TreeNode(2, null, node1);
24-
25-
TreeNode node5 = new TreeNode(5, node3, node2);
26-
TreeNode nodef3 = new TreeNode(-3, null, node11);
27-
28-
TreeNode root = new TreeNode(10, node5, nodef3);
29-
30-
// List<Integer> list = new BinaryTreePreorderTraversal().preorderTraversal(root);
31-
// for (Integer n : list) {
32-
// System.out.print(n + " ");
33-
// }
34-
35-
List<String> paths = pathSum(root, 8);
36-
for (String s : paths) {
37-
System.out.println(s);
38-
}
12+
boolean result = canArrangeWords(new String[] {
13+
"abc", "ce", "ef", "fu", "cd", "dx", "xc"
14+
});
15+
System.out.print(result);
3916
}
4017

41-
public static List<String> pathSum(TreeNode root, int sum) {
42-
List<String> result = new LinkedList<>();
43-
pathSum(root, sum, result, "");
44-
return result;
18+
public static int perfectNumberCheck(int n) {
19+
int k = n;
20+
for (int i = 1; i < n; i++) {
21+
if (n % i == 0) {
22+
k -= i;
23+
}
24+
}
25+
return k == 0 ? 1 : 0;
4526
}
4627

47-
private static void pathSum(TreeNode root, int sum, List<String> list, String path) {
48-
if (root == null) {
49-
return;
28+
public static boolean canArrangeWords(String[] words) {
29+
List<String> set = new ArrayList<String>();
30+
if (helper(null, words, set)) {
31+
for (String s : set) {
32+
System.out.print(s + "->");
33+
}
34+
return true;
5035
}
51-
52-
pathSumWithRoot(root, sum, list, path);
53-
54-
pathSum(root.left, sum, list, "");
55-
pathSum(root.right, sum, list, "");
36+
return false;
5637
}
5738

58-
private static void pathSumWithRoot(TreeNode root, int sum, List<String> list, String path) {
59-
if (root == null) {
60-
return;
39+
public static boolean helper(String word, String[] words, List<String> set) {
40+
if (set.size() == words.length) {
41+
return true;
6142
}
6243

63-
String prefix = path.isEmpty() ? "" : path + "->";
64-
65-
if (root.val == sum) {
66-
list.add(prefix + root.val);
44+
for (String s : words) {
45+
if (!set.contains(s) && (word == null || s.charAt(0) == word.charAt(word.length() - 1))) {
46+
set.add(s);
47+
if (helper(s, words, set)) {
48+
return true;
49+
}
50+
set.remove(s);
51+
}
6752
}
68-
pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val);
69-
pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val);
53+
return false;
7054
}
7155
}
7256

0 commit comments

Comments
 (0)