MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

C++/Find-The-Difference.cpp:10–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9
10class Solution {
11public:
12 char findTheDifference(string s, string t) {
13
14 char returnChar= '\0';
15 std::unordered_map <char,int>ourMap;
16
17 //count the letters in the input string and add it to map
18 for (const char& letter:s) {
19
20 if(ourMap.find(letter)==ourMap.end()){ ourMap[letter]=1;}
21 else{ourMap[letter]++;}
22 }
23
24
25 //compare with letters in other string
26 for (const char& letter:t){
27
28 //if you can find the letter then decrement it
29 if(ourMap.find(letter)!=ourMap.end()) {
30
31 ourMap[letter]--;
32 if(ourMap[letter]<0) { return letter; }
33
34 }
35
36 else{return letter;}
37 }
38
39 return returnChar;
40
41 }
42};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected