Why am I getting "Runtime Error: AddressSanitizer: DEADLY SIGNAL"?

Viewed 1195

While trying to solve this leetcode problem, I was getting a Runtime Error: AddressSanitizer: DEADLY SIGNAL.

The complete Error message is

AddressSanitizer:DEADLYSIGNAL
=================================================================
==30==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x0000002b0452 bp 0x7ffd8e6682e0 sp 0x7ffd8e667a78 T0)
==30==The signal is caused by a READ memory access.
==30==Hint: this fault was caused by a dereference of a high value address (see register values below).  Dissassemble the provided pc to learn which register was used.
    #3 0x7f73671f7d6b  (/lib/x86_64-linux-gnu/libstdc++.so.6+0x144d6b)
    #10 0x7f7366d6b0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
AddressSanitizer can not provide additional info.
==30==ABORTING

Problem Description:

Given a "pattern" and a string "s", find if "s" follows the same "pattern".

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

Example 1:

Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Example 2:

Input: pattern = "abba", s = "dog cat cat fish"
Output: false

Here is my solution:

class Solution {
    public:
    bool wordPattern(string pattern, string s) {
        vector<string>vec;
        string tmp = "";
        for(int i=0 ; i<s.size() ; i++){
            if(s[i] == ' '){
                vec.push_back(tmp);
                tmp.clear();
                continue;
            }
            tmp += s[i];
        }
        map<char , set<string> > mp;
        for(int i=0 ; i<pattern.size() ; i++){
            mp[pattern[i]].insert(vec[i]);
        }
        bool flag = 0;
        for(int i=0 ; i<pattern.size() ; i++){
            char x = pattern[i];
            if(mp[x].size() != 1)
                flag = 1;
        }
        return !flag;
    }
};
0 Answers
Related