In this Leetcode Design Twitter problem solution Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the 10 most recent tweets in the user's news feed.

Implement the Twitter class:

  1. Twitter() Initializes your twitter object.
  2. void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId.
  3. List<Integer> getNewsFeed(int userId) Retrieves the 10 most recent tweet IDs in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user themself. Tweets must be ordered from most recent to least recent.
  4. void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId.
  5. void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing the user with ID followeeId.

Leetcode Design Twitter problem solution


Problem solution in Python.

class Twitter:
    def __init__(self):
        self.d={}
        self.f={}
        self.o=[]
    def postTweet(self, a: int, b: int) -> None:
        if a in self.d:
            self.d[a]=[b]+self.d[a]
            print(self.d[a])
        else:
            self.d[a]=[b]
        self.o=[[a,b]]+self.o
    def getNewsFeed(self, a: int) -> List[int]:
        fol=[]
        if a in self.f:
            fol=self.f[a]
        res=[];c=0;
        for i,j in self.o:
            if i==a or  i in fol:
                res.append(j)
                c+=1
            if c>=10:
                break
        return res
    def follow(self, a: int, b: int) -> None:
        if a in self.f:
            self.f[a].append(b)
        else:
            self.f[a]=[b]

    def unfollow(self, a: int, b: int) -> None:
        if a in self.f:
            if b in self.f[a]:
                self.f[a].remove(b)



Problem solution in Java.

public class Twitter {
    public class Tweet{
        int id; int userId;
        public Tweet (int id, int userId) { this.id = id; this.userId = userId; }
    }
    
    LinkedList<Tweet> tweetStack= new LinkedList<>();
    HashMap<Integer, HashSet<Integer>> users = new HashMap<>();

    public void postTweet(int userId, int tweetId) {
        if (!users.containsKey(userId)) users.put(userId, new HashSet<>());
        tweetStack.push(new Tweet(tweetId, userId));
    }

    public List<Integer> getNewsFeed(int userId) {
        if (!users.containsKey(userId)) users.put(userId, new HashSet<>());
        List<Integer> result = new ArrayList<>();
        for (Tweet tweet : tweetStack){
            if (users.get(userId).contains(tweet.userId) || tweet.userId == userId) result.add(tweet.id);
            if (result.size() == 10) break;
        }
        return result;
    }
    
    public void follow(int followerId, int followeeId) {
        if (!users.containsKey(followerId)) users.put(followerId, new HashSet<>());
        users.get(followerId).add(followeeId);
    }
    
    public void unfollow(int followerId, int followeeId) {
        if (!users.containsKey(followerId)) users.put(followerId, new HashSet<>());
        users.get(followerId).remove(followeeId);
    }
}


Problem solution in C++.

class Twitter {
public:

unordered_set<int> a={};
vector<unordered_set<int>> followers;
vector<pair<int,int>> news;
Twitter() {
    for(int i=0;i<10000;++i)
        followers.push_back({i});
}


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

vector<int> getNewsFeed(int userId) {
    vector<int> res;
    int n=news.size();
    for(int i=n-1;(i>=0&&res.size()<10);--i)
        if(followers[userId].find(news[i].first)!=followers[userId].end())
            res.push_back(news[i].second);
    return res;
}


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


void unfollow(int followerId, int followeeId) {
    if(followerId!=followeeId)
    followers[followerId].erase(followeeId);
}
};