389.Find the Difference
2017-04-11 15:29
197 查看
Description:
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
[cpp] view
plain copy
class Solution {
public:
char findTheDifference(string s, string t){
int m = s.length() ;
int n = t.length() ;
char res ;
vector<int> letter(26, 0) ;
for(int i = 0 ; i < m ; i ++)
{
letter[s[i] - 'a'] ++ ;
}
for(int i = 0 ; i < n ; i ++)
{
letter[t[i] - 'a'] -- ;
}
for(int i = 0 ; i < 26 ; i ++)
{
if(letter[i] < 0)
{
res = i + 'a';
break ;
}
}
return res ;
}
};
[cpp] view plain copy class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> letter(26 , 0) ;
int m = magazine.size() ;
int n = ransomNote.size() ;
for(int i = 0 ; i < n ; i ++)
{
letter[ransomNote[i] - 'a'] ++ ;
}
for(int i = 0 ; i < m ; i ++)
{
letter[magazine[i] - 'a'] -- ;
}
for(int i = 0 ; i < 26 ; i ++)
{
if(letter[i] > 0)
{
return false ;
}
}
return true ;
}
};
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.
分析:
首先要确定不能被这一个样例哄骗了。s是随机产生的,t在s的基础上加上一个字母,字母的位置也是随机的。
My C++ code:
[cpp] view
plain copy
class Solution {
public:
char findTheDifference(string s, string t){
int m = s.length() ;
int n = t.length() ;
char res ;
vector<int> letter(26, 0) ;
for(int i = 0 ; i < m ; i ++)
{
letter[s[i] - 'a'] ++ ;
}
for(int i = 0 ; i < n ; i ++)
{
letter[t[i] - 'a'] -- ;
}
for(int i = 0 ; i < 26 ; i ++)
{
if(letter[i] < 0)
{
res = i + 'a';
break ;
}
}
return res ;
}
};
383.Ransom Note
Description:
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note: You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
分析:
套路和上一道题目是一样的,注意要分清楚magazines和randsomNote。
My C++ code:
[cpp] view plain copy class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> letter(26 , 0) ;
int m = magazine.size() ;
int n = ransomNote.size() ;
for(int i = 0 ; i < n ; i ++)
{
letter[ransomNote[i] - 'a'] ++ ;
}
for(int i = 0 ; i < m ; i ++)
{
letter[magazine[i] - 'a'] -- ;
}
for(int i = 0 ; i < 26 ; i ++)
{
if(letter[i] > 0)
{
return false ;
}
}
return true ;
}
};
相关文章推荐
- LeetCode 389. Find the Difference 题解(C++)
- Problem 389. Find the Difference
- 389. Find the Difference
- leetcode 389 Find the Difference
- 389.Find the Difference
- 【LeetCode-389】Find the Difference(C++)
- 389. Find the Difference*
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- LeetCode - 389. Find the Difference
- 389. Find the Difference
- Leetcode 389 Find the Difference
- [leetcode]: 389. Find the Difference
- leetcode389 Find the Difference java
- LeetCode_389-Find the Difference
- Leetcode 389 Find the Difference
- Leetcode 389. Find the Difference
- leetcode-389. Find the Difference 字典,查找某个元素a不在list中
- leetcode刷题系列-389. Find the Difference