Advertisement
sanya5791

Amazon: Concatenated Words (wrong solution on Interview)

Aug 9th, 2021
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.67 KB | None | 0 0
  1. class Solution {
  2.    
  3.     fun findAllConcatenatedWordsInADict(words: Array<String>): List<String> {
  4.         val result: MutableList<String> = LinkedList()
  5.        
  6.         val set = words.toSet()
  7.         words.forEach { word ->
  8.             var wordsCnt = 0
  9.             var concatWord = ""
  10.             var part = ""
  11.             word.forEach { ch ->
  12.                 part += ch
  13.                 if(set.contains(part)) {
  14.                     concatWord += part
  15.                     wordsCnt++
  16.                     part = ""
  17.                 }
  18.             }
  19.             if(part.isEmpty() && wordsCnt > 1) result.add(concatWord)
  20.         }
  21.        
  22.         return result
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement