LeetCode, GeeksForGeeks Problem of the day solution
الذهاب إلى القناة على Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
إظهار المزيد1 250
المشتركون
+224 ساعات
+147 أيام
+2930 أيام
أرشيف المشاركات
class Solution {
public:
int maximumElementAfterDecrementingAndRearranging(vector& arr) {
sort(arr.begin(),arr.end());
int res = 1;
for(int i=1; i res)
res = res + 1;
}
return res;
}
};
class Solution {
public:
int count(string s)
{
int n=s.length();
vector last(256,-1);
int dp[n+1];
dp[0]=1;
for(int i=1;icount(str1)? str2:str1;
}
};
class Solution {
public:
int countPalindromicSubsequence(string s)
{
int n = s.length();
vector>indices(26,{-1,-1});
for(int i=0 ; ist;
for(int j = left+1 ; j
class Solution
{
public:
//Function to check if two strings are rotations of each other or not.
bool areRotations(string s1,string s2)
{
// Your code here
int n=s1.length();
for(int i=0;i
class Solution {
public:
string sortVowels(string s) {
priority_queue<char, vector<char>, greater<char>> temp;
int n = s.size();
for(int i=0;i<n;i++){
char x = s[i];
if (x == 'a' or x == 'e' or x == 'i' or
x == 'o' or x == 'u' or x == 'A' or x == 'E' or x == 'I' or
x == 'O' or x == 'U'){
temp.push(x);
}
}
string ans = "";
for(int i=0;i<n;i++){
char x = s[i];
if (x == 'a' or x == 'e' or x == 'i' or
x == 'o' or x == 'u' or x == 'A' or x == 'E' or x == 'I' or
x == 'O' or x == 'U'){
ans += temp.top();
temp.pop();
}
else{
ans += s[i];
}
}
return ans;
}
};
class Solution
{
public:
//Function to find length of shortest common supersequence of two strings.
int shortestCommonSupersequence(string X, string Y, int m, int n)
{
//code here
int dp[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0)
dp[i][j]=j;
if(j==0)
dp[i][j]=i;
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(X[i-1]==Y[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1;
}
}
return(dp[m][n]);
}
};
class Solution {
public://use modified bfs
int numBusesToDestination(vector>& routes, int source, int target) {
if(source == target) return 0;
int n = routes.size();
unordered_map> adj;//(bus stop, (bus no/ind))
for(int i = 0; i < n; i++){
for(auto &stop: routes[i]){
adj[stop].push_back(i);
}
}
vector vis(501, 0);
queue q;
for(auto &route: adj[source]){
q.push(route);
vis[route] = 1;
}
int cnt = 1;//for the source bus
while(!q.empty()){
int size = q.size();
while(size--){
int bus_no = q.front();
q.pop();
for(auto &stop: routes[bus_no]){
if(stop == target){
return cnt;
}
for(auto &bus_stops: adj[stop]){
if(!vis[bus_stops]){
vis[bus_stops] = 1;
q.push(bus_stops);
}
}
}
}
cnt++;
}
return -1;
}
};
class Solution
{
public:
//Function to check if a string can be obtained by rotating
//another string by exactly 2 places.
bool isRotated(string str1, string str2)
{
if(str2.size()!=str1.size()) return false;
string copy = str1;
int n = str1.size();
string temp;
temp ="";
temp.push_back(str1[n - 2]);
temp.push_back(str1[n - 1]);
str1.pop_back();
str1.pop_back();
temp+=str1;
// cout<
class Graph {
public:
// int temp;
#define ll long long
map<ll, vector<pair<ll, ll>> > adj;
int temp;
int dijkstra(int V, map<ll, vector<pair<ll, ll>> > adj, int S, int D)
{
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>> > pq;
pq.push({0, S});
vector<ll> dist(V);
for(int i=0;i<V;i++){
dist[i] = 1e9;
}
dist[S] = 0;
while(pq.empty()==false){
ll dis = pq.top().first;
ll node = pq.top().second;
pq.pop();
for(auto it:adj[node]){
ll adjNode = it.first;
ll wt = it.second;
if(dis+wt<dist[adjNode]){
dist[adjNode] = dist[node] + wt;
pq.push({dist[adjNode], adjNode});
}
}
}
// cout<<dist[D]<<endl;
if(dist[D]==1e9){
return -1;
}
return dist[D];
}
Graph(int n, vector<vector<int>>& edges) {
temp=n;
for(int i=0;i<edges.size();i++){
adj[edges[i][0]].push_back({edges[i][1], edges[i][2]});
}
}
void addEdge(vector<int> edge) {
// temp++;
adj[edge[0]].push_back({edge[1], edge[2]});
}
int shortestPath(int node1, int node2) {
// cout<<temp<<endl;
return dijkstra(temp, adj, node1, node2);
}
};
class Solution
{
public:
//Function to check if two strings are isomorphic.
bool areIsomorphic(string str1, string str2)
{
if (str1.size() != str2.size())
{
return 0;
}
map a, b;
for(auto&x:str1)a[x]++;
for(auto&x:str2)b[x]++;
vector aa, bb;
for(auto&x:a)aa.push_back(x.second);
for(auto&x:b)bb.push_back(x.second);
sort(aa.begin(),aa.end());
sort(bb.begin(),bb.end());
if (aa != bb) return 0;
map m;
for(int i=0; i
