forked from dylansun/leetcode-cn-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo648.scala
56 lines (50 loc) · 1.35 KB
/
No648.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* 648. 单词替换
*/
object No648 {
object BruteForce {
def replaceWords(l: List[String], sentence: String): String = {
val dict = l.sortBy{x => x.length}
def f(str:String):String = dict dropWhile {x => !str.startsWith(x)} sortBy {x => x.length} match {
case Nil => str
case h::t => h
}
sentence split (" ") map f mkString(" ")
}
}
object UseTrie{
class Trie {
var endsHere = false
val next = Array.ofDim[Trie](26)
def insert(str:String):Unit = {
var node = this
for{ ch <- str}{
if(node.next(ch - 'a') != null) node = node.next(ch - 'a')
else {
node.next(ch-'a') = new Trie()
node = node.next(ch - 'a')
}
}
node.endsHere = true
}
def fetch(str:String):String = {
var node = this
var l = List.empty[Char]
for { ch <- str}{
if(node.endsHere) return l.reverse.mkString
if(node.next(ch - 'a') == null) return str
node = node.next(ch - 'a')
l ::= ch
}
str
}
def replaceWords(l: List[String], sentence: String): String = {
val trie = new Trie()
l foreach trie.insert
sentence split " " map trie.fetch mkString " "
}
}
}
def main(args: Array[String]): Unit = {
}
}