File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
algorithms/cpp/findTheDifference Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/find-the-difference/
2+ // Author : Hao Chen
3+ // Date : 2016-09-08
4+
5+ /* **************************************************************************************
6+ *
7+ * Given two strings s and t which consist of only lowercase letters.
8+ *
9+ * String t is generated by random shuffling string s and then add one more letter at a
10+ * random position.
11+ *
12+ * Find the letter that was added in t.
13+ *
14+ * Example:
15+ *
16+ * Input:
17+ * s = "abcd"
18+ * t = "abcde"
19+ *
20+ * Output:
21+ * e
22+ *
23+ * Explanation:
24+ * 'e' is the letter that was added.
25+ ***************************************************************************************/
26+
27+ class Solution {
28+ public:
29+ char findTheDifference (string s, string t) {
30+ unordered_map<char , int > m;
31+ for (auto c : s) m[c]++;
32+ for (auto c : t) {
33+ m[c]--;
34+ if (m[c] < 0 ) return c;
35+ }
36+ return ' \0 ' ;
37+ }
38+ };
You can’t perform that action at this time.
0 commit comments