-1

The size of unordered map keeps increasing in the last for loop? Why is it so?

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size() != t.size()){
            return false;
        }
        
        unordered_map<char,int> sm;
        unordered_map<char,int> tm;
        for (int i = 0; i<s.size();i++){
            sm[s[i]]++;
            tm[t[i]]++;
        }
        for (int j = 0; j<sm.size();j++){
            cout << j << endl;
            cout << sm.size() <<endl;
            if(sm[j] != tm[j]){
                cout << sm[j] << endl;
                cout << tm[j] << endl;
                cout << j << endl;
                return false;
            }
        }
        return true;
    }
};

I have tried to search online but can't find any relevant answer.

Jayden
  • 25
  • 7
  • Maybe because the `operator[]` of the unordered map does an insertion each time when called. If you only want to access an element's value then use the [at](https://en.cppreference.com/w/cpp/container/unordered_map/at) member function. – digito_evo Feb 08 '23 at 16:47
  • 2
    `sm[j] != tm[j]` inserts elements if they are not in the map(s) already. – Ted Lyngmo Feb 08 '23 at 16:47
  • Your ```sm``` and ```tm``` are `````` unordered_maps, yet you keep indexing it with integers in your final for loop. I guess you would want to iterate through its elements and not index it with numbers. – Phoenixdust Feb 08 '23 at 16:47
  • 1
    You were scammed by one of many sites that do not teach qualitative software development. – 273K Feb 08 '23 at 16:51
  • @273K Probably *FeetCode*. At least that's what I call that harmful website! – digito_evo Feb 08 '23 at 16:53
  • [Demo](https://godbolt.org/z/bhxME8dzv) of a much simpler algorithm that works. – Ted Lyngmo Feb 08 '23 at 16:58
  • You can just compare `sm == tm` and do noty need that second loop at all. – Slava Feb 08 '23 at 17:02
  • @Slava or just use one map. Add from `s` and subtract from `t` and all mapped `char`s should be mapped to `0` afterwards. – Ted Lyngmo Feb 08 '23 at 17:04
  • @TedLyngmo sure, but that would be next step - optimization. Next step would be not to use map but array of 26 elements – Slava Feb 08 '23 at 17:06
  • 1
    @Slava True. Another alternative: `std::sort(s.begin(), s.end()); std::sort(t.begin(), t.end()); return s == t;` – Ted Lyngmo Feb 08 '23 at 17:10
  • @273K Do you have any recommendation for someone who is starting learning cpp? – Jayden Feb 09 '23 at 03:09
  • I sometimes see this online resource https://www.learncpp.com/ is taken positively here. You should not pass [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for sure. – 273K Feb 09 '23 at 03:22

1 Answers1

2

https://en.cppreference.com/w/cpp/container/unordered_map/operator_at

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

sm[j] != tm[j]

The [] operator on maps inserts entries if they didn't already exist.

You probably wanted to call .find(j) instead

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158