-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yifeng Li
authored and
Yifeng Li
committed
Oct 23, 2018
1 parent
8175548
commit 7f11e0d
Showing
3 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import java.util.HashMap; | ||
import java.util.ArrayList; | ||
|
||
class MagicDictionary { | ||
|
||
HashMap<String,ArrayList<String>> mdict; | ||
|
||
/** Initialize your data structure here. */ | ||
public MagicDictionary() { | ||
mdict = new HashMap<String,ArrayList<String>>(); | ||
} | ||
|
||
/** Build a dictionary through a list of words */ | ||
public void buildDict(String[] dict) { | ||
|
||
for (String str : dict) { | ||
String[] strarray = buildStrArray(str); | ||
|
||
for (String var : strarray) { | ||
|
||
if(mdict.keySet().contains(var)) | ||
{ | ||
ArrayList<String> list = mdict.get(var); | ||
list.add(str); | ||
} | ||
else | ||
{ | ||
ArrayList<String> list = new ArrayList<String>(); | ||
list.add(str); | ||
mdict.put(var,list); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */ | ||
public boolean search(String word) { | ||
|
||
String[] strarray = buildStrArray(word); | ||
|
||
for (String var : strarray) { | ||
if(mdict.keySet().contains(var)) | ||
{ | ||
|
||
ArrayList<String> list = mdict.get(var); | ||
|
||
|
||
if(list.size() > 1) | ||
return true; | ||
|
||
if(!list.get(0).equals(word)) | ||
return true; | ||
|
||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public String[] buildStrArray(String str) | ||
{ | ||
char[] array = str.toCharArray(); | ||
|
||
String[] r = new String[array.length]; | ||
|
||
for(int i = 0; i<array.length;i++) | ||
{ | ||
char t = array[i]; | ||
array[i] = '?'; | ||
r[i] = new String(array); | ||
array[i] = t; | ||
|
||
} | ||
|
||
return r; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* OddEvenLinkedList | ||
*/ | ||
public class OddEvenLinkedList { | ||
|
||
public ListNode oddEvenList(ListNode head) { | ||
|
||
if(head == null || head.next == null) | ||
return head; | ||
|
||
ListNode head2 = head.next; | ||
|
||
ListNode evenListHead = head; | ||
ListNode oddListHead = head.next; | ||
|
||
|
||
while(evenListHead.next != null && oddListHead.next != null) | ||
{ | ||
if(evenListHead.next.next != null) | ||
{ | ||
ListNode Next = evenListHead.next.next; | ||
evenListHead.next = Next; | ||
evenListHead = Next; | ||
} | ||
else | ||
{ | ||
evenListHead.next = null; | ||
} | ||
|
||
if(oddListHead.next.next != null) | ||
{ | ||
ListNode Next = oddListHead.next.next; | ||
oddListHead.next = Next; | ||
oddListHead = Next; | ||
} | ||
else | ||
{ | ||
oddListHead.next = null; | ||
} | ||
} | ||
|
||
evenListHead.next = head2; | ||
|
||
return head; | ||
} | ||
} |