Skip to content

Commit ba25569

Browse files
Merge pull request algorithm001#718 from artisticman/master
第四周操作作业,学号140
2 parents 0f96701 + 3a58f4b commit ba25569

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
3+
*
4+
* If there is no answer, return the empty string.
5+
* Example 1:
6+
* Input:
7+
* words = ["w","wo","wor","worl", "world"]
8+
* Output: "world"
9+
* Explanation:
10+
* The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
11+
* Example 2:
12+
* Input:
13+
* words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
14+
* Output: "apple"
15+
* Explanation:
16+
* Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
17+
* Note:
18+
*
19+
* All the strings in the input will only contain lowercase letters.
20+
* The length of words will be in the range [1, 1000].
21+
* The length of words[i] will be in the range [1, 30].
22+
*/
23+
public class Leetcode_720_140 {
24+
25+
class TrieNode{
26+
char val;
27+
TrieNode[] next;
28+
boolean isWord;
29+
TrieNode(char val){
30+
this.val = val;
31+
next = new TrieNode[26];
32+
isWord = false;
33+
}
34+
}
35+
public String longestWord(String[] words) {
36+
TrieNode root = new TrieNode(' ');
37+
for(String str: words){
38+
buildTree(root, str);
39+
}
40+
int max = 0;
41+
int index = -1;
42+
for(int i = 0; i < words.length; i++){
43+
String str = words[i];
44+
boolean flag = findWord(root, str);
45+
if(flag && str.length() > max){
46+
max = str.length();
47+
index = i;
48+
}
49+
if(flag && str.length() == max){
50+
String old = words[index];
51+
index = str.compareTo(old) < 0 ? i : index;
52+
}
53+
}
54+
return words[index];
55+
}
56+
private void buildTree(TrieNode root, String str){
57+
for(char c : str.toCharArray()){
58+
if(root.next[c - 'a'] == null){
59+
root.next[c - 'a'] = new TrieNode(c);
60+
}
61+
root = root.next[c - 'a'];
62+
}
63+
root.isWord = true;
64+
}
65+
private boolean findWord(TrieNode root, String str){
66+
for(int i = 0; i < str.length(); i++){
67+
char c = str.charAt(i);
68+
if(root.next[c - 'a'] == null){
69+
return false;
70+
}
71+
root = root.next[c - 'a'];
72+
if(!root.isWord) {
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
79+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
5+
/**
6+
* Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.
7+
*
8+
* Examples:
9+
* Input: S = "a1b2"
10+
* Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
11+
*
12+
* Input: S = "3z4"
13+
* Output: ["3z4", "3Z4"]
14+
*
15+
* Input: S = "12345"
16+
* Output: ["12345"]
17+
* Note:
18+
*
19+
* S will be a string with length between 1 and 12.
20+
* S will consist only of letters or digits.
21+
*/
22+
public class Leetcode_784_140 {
23+
24+
public List<String> letterCasePermutation(String S) {
25+
26+
List<String> s = new ArrayList<String>();
27+
if (S == null) {
28+
return new LinkedList<>();
29+
}
30+
31+
util(S.toCharArray(), s, 0);
32+
return s;
33+
}
34+
35+
public void util(char[] c_arr, List<String> s, int index) {
36+
if(index == c_arr.length) {
37+
s.add(new String(c_arr));
38+
return;
39+
}
40+
41+
if(Character.isDigit(c_arr[index])) {
42+
util(c_arr,s,index+1);
43+
return;
44+
}
45+
46+
c_arr[index] = Character.toLowerCase(c_arr[index]);
47+
util(c_arr,s, index+1);
48+
49+
c_arr[index] = Character.toUpperCase(c_arr[index]);
50+
util(c_arr,s,index+1);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)