data-structures-and-algorithms

This repository is going to be for problem solving tasks provided by ASAC team - Java Advanced course

View project on GitHub

Challenge Summary

This challenge asks us to implement a function that takes a string and returns the first duplicated word in that string.

Whiteboard Process

hashmap-duplicated-words whiteboard

Approach & Efficiency

To solve this challenge I started by splitting the string into words then I mapped each word with an integer representing its frequency inside the string and when I find a word that is duplicated I return it to be the answer. This function takes O(n) space complexity since I declare a new array of strings to hold the words and O(n) speed complexity since I loop over all n words inside the string. You can view the code from here

Solution

To use this function you can simply call it and send a string inside it as follows: HashmapRepeatedWord(string)
or you can run ./gradlew run to run the test cases.