Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Jongeun/Day20/131_PalindromePartitioning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution
{
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> res;
vector<string> sub;
_partition(res, sub, s);
return res;
}

void _partition(vector<vector<string>> &res, vector<string> &sub, string s)
{
if (isPalindrom(s))
{
sub.push_back(s);
res.push_back(sub);
sub.pop_back();
}

for (int i = 0; i < s.size() - 1; i++)
{
string left = s.substr(0, i + 1);
if (isPalindrom(left))
{
sub.push_back(left);
_partition(res, sub, s.substr(i + 1));
sub.pop_back();
}
}
}

bool isPalindrom(string &s)
{
int l = 0;
int r = s.size() - 1;
while (l <= r)
{
if (s[l] == s[r])
{
l++;
r--;
}
else
{
return false;
}
}

return true;
}
};
42 changes: 42 additions & 0 deletions Jongeun/Day20/17_LetterCombinationsofaPhoneNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution
{
public:
vector<string> letterCombinations(string digits)
{

// construct the map
unordered_map<char, string> m;
m['2'] = "abc";
m['3'] = "def";
m['4'] = "ghi";
m['5'] = "jkl";
m['6'] = "mno";
m['7'] = "pqrs";
m['8'] = "tuv";
m['9'] = "wxyz";

vector<string> res;
if (digits == "")
{
return res;
}

_letterCombinations(res, digits, m, "", 0);
return res;
}

void _letterCombinations(vector<string> &res, string &digits, unordered_map<char, string> &m, string comb, int start)
{
if (start == digits.size())
{
res.push_back(comb);
return;
}

for (auto c : m[digits[start]])
{

_letterCombinations(res, digits, m, comb + c, start + 1);
}
}
};
56 changes: 56 additions & 0 deletions Jongeun/Day20/355_DesignTwitter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Twitter
{
public:
Twitter()
{
}

void postTweet(int userId, int tweetId)
{
tweets.push_back({userId, tweetId});
}

vector<int> getNewsFeed(int userId)
{
vector<int> res;
int num = 10;
unordered_set<int> temp = followMap[userId];
for (int i = tweets.size() - 1; i >= 0; i--)
{
if (tweets[i].first == userId || temp.find(tweets[i].first) != temp.end())
{
res.push_back(tweets[i].second);
num--;
}
if (num == 0)
{
break;
}
}

return res;
}

void follow(int followerId, int followeeId)
{
followMap[followerId].insert(followeeId);
}

void unfollow(int followerId, int followeeId)
{
followMap[followerId].erase(followeeId);
}

private:
unordered_map<int, unordered_set<int>> followMap;
vector<pair<int, int>> tweets;
};

/**
* Your Twitter object will be instantiated and called as such:
* Twitter* obj = new Twitter();
* obj->postTweet(userId,tweetId);
* vector<int> param_2 = obj->getNewsFeed(userId);
* obj->follow(followerId,followeeId);
* obj->unfollow(followerId,followeeId);
*/
Loading