As today's question LC-884 is very easy..i am just posting the code LC-884 Uncommon words from two sentences link to ques-https://leetcode.com/problems/uncommon-words-from-two-sentences/description/?envType=daily-question&envId=2024-09-17
1st APPROACH- class Solution {
public:
vector<string> uncommonFromSentences(string s1, string s2) {
vector<string> ans;
unordered_map<string,int> mp;
int i=0,j=0;
string temp="";
AAO-CODING-KARE
As today's question LC-884 is very easy..i am just posting the code
LC-884 Uncommon words from two sentences
link to ques-https://leetcode.com/problems/uncommon-words-from-two-sentences/description/?envType=daily-question&envId=2024-09-17
1st APPROACH-
class Solution {
public:
vector<string> uncommonFromSentences(string s1, string s2) {
vector<string> ans;
unordered_map<string,int> mp;
int i=0,j=0;
string temp="";
while(j<s1.length()){
if(s1[j]==' '){
mp[s1.substr(i,j-i)]++;
i=j+1;
}
j++;
}
mp[s1.substr(i,j-i)]++;
j=0;
i=0;
while(j<s2.length()){
if(s2[j]==' '){
mp[s2.substr(i,j-i)]++;
i=j+1;
}
j++;
}
mp[s2.substr(i,j-i)]++;
for(auto i:mp){
if(i.second==1){
ans.push_back(i.first);
}
}
return ans;
}
};
2nd APPROACH
class Solution {
public:
vector<string> uncommonFromSentences(string s1, string s2) {
vector<string> ans;
unordered_map<string,int> mp;
stringstream ss(s1);
stringstream SS(s2);
string token;
while(getline(ss,token,' ')){
mp[token]++;
}
while(getline(SS,token,' ')){
mp[token]++;
}
for(auto i:mp){
if(i.second==1){
ans.push_back(i.first);
}
}
return ans;
}
};
1 year ago | [YT] | 3
View 0 replies