LeetCode题解之Find the Difference

1、题目描述

2、题目分析

比较两个字符串中加入的一个字符,由于可能在字符串中加入一个已经存在的字符,因此使用hash table 去统计字符个数最好。

3、代码

 1  char findTheDifference(string s, string t) {
 2         
 3         map<char , int> m;
 4         for( auto & c : s )
 5             m[c]++;
 6         map<char , int>mt;
 7         for( auto &c : t)
 8             mt[c]++;
 9         
10         for( map<char,int>::iterator it = mt.begin() ; it != mt.end() ; it++ )
11         {
12             if( it->second != m[it->first] )
13                 return it->first;
14         }
15         
16     }

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/9288958.html