Skip to content

Commit 56f7e79

Browse files
committed
one more exercise
1 parent d7cfc70 commit 56f7e79

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.codecraftlabs.leetcode
2+
3+
import scala.collection.mutable
4+
5+
object SubDomainVisitCount {
6+
def subDomainVisits(domains: Array[String]): List[String] = {
7+
val mappings = mutable.Map[String, Long]()
8+
domains.foreach(i => splitDomains(i, mappings))
9+
mappings.map(i => i._2.toString + " " + i._1).toList
10+
}
11+
12+
private def splitDomains(domain: String, mapping: mutable.Map[String, Long]): Unit = {
13+
val items = domain.split(" ").map(_.trim)
14+
val counter = items(0).toLong
15+
val url = items(1)
16+
17+
val urlPieces = url.split("\\.").map(_.trim).reverse
18+
var buffer: String = ""
19+
urlPieces.foreach(item => {
20+
val temp = item + "."
21+
buffer = temp + buffer
22+
val currentValue: Long = mapping.getOrElse(buffer.toString().stripPrefix("."), 0)
23+
val total = currentValue + counter
24+
mapping.put(buffer.toString().stripPrefix("."), total)
25+
})
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.codecraftlabs.leetcode
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
5+
class SubDomainVisitCountSpec extends FlatSpec with Matchers {
6+
"Test case 001" should "return true" in {
7+
val items = Array("500 test.com")
8+
val result = SubDomainVisitCount.subDomainVisits(items)
9+
}
10+
}

0 commit comments

Comments
 (0)