본문 바로가기

Computer Science/Algorithm with Code

Hacker Rank_4/54 tests (Easy, Sparse Array)

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.

 

Example

queries = [a', 'b', c'd']

strings = [a', b, c'd']

return => [1, 0, 1]

 

func matchingStrings(strings: [String], queries: [String]) -> [Int] {
    // Hashmap - Dictionary // Arr - size/order
    //var dics = [String : Int]()
    
    var arr = [Int](0..<queries.count)
    
    for i in 0..<queries.count {
        arr[i] = 0
        for j in strings {    
            if queries[i] == j {
                arr[i] += 1
            }
        }
    }
    
    return arr
}