Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a3378c

Browse files
.
1 parent b2b3704 commit 9a3378c

17 files changed

+543
-21
lines changed

‎Decode_The_Message.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/* https://leetcode.com/problems/decode-the-message/ */
3+
4+
class Solution {
5+
public:
6+
string decodeMessage(string key, string msg) {
7+
8+
char c='a';
9+
unordered_map<char,char>m;
10+
for(int i=0;i<key.length();i++)
11+
{
12+
if(key[i]==' ' || m.find(key[i])!=m.end())
13+
{ continue;
14+
}
15+
else{
16+
m[key[i]]=c;
17+
c++;
18+
}
19+
}
20+
string ans;
21+
22+
for(auto i:msg)
23+
{
24+
if(m.find(i)!=m.end() && i!=' ')
25+
{
26+
ans+= m[i];
27+
}
28+
else
29+
{
30+
ans+=" ";
31+
}
32+
}
33+
for(auto i:m)
34+
{
35+
cout<<i.first<<" " << i.second<<endl;
36+
}
37+
return ans;
38+
39+
}
40+
};

‎Longest_common_prefix.cpp‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*https://leetcode.com/problems/longest-common-prefix/description/*/
2+
3+
4+
/*optimal*/
5+
class Solution {
6+
public:
7+
string longestCommonPrefix(vector<string>& s) {
8+
sort(s.begin(),s.end());
9+
string s1=s[0];
10+
string s2=s[s.size()-1];
11+
string ans="";
12+
for(int i=0;i<s1.length();i++)
13+
{
14+
if(s1[i]==s2[i])
15+
{
16+
ans+=s1[i];
17+
}
18+
else
19+
{
20+
return ans;
21+
}
22+
}
23+
return ans;
24+
}
25+
};
26+
27+
28+
29+
30+
31+
/*greedy */
32+
class Solution {
33+
public:
34+
string longestCommonPrefix(vector<string>& s) {
35+
if(s.empty()) return "";
36+
string temp=s[0];
37+
int mini=INT_MAX;
38+
string ans="";
39+
for(int i=1;i<s.size();i++)
40+
{ int count=0;
41+
for(int j=0;j<s[i].size();j++)
42+
{
43+
if(s[0][j]==s[i][j])
44+
{
45+
count++;
46+
}
47+
}
48+
49+
mini=min(mini,count);
50+
51+
}
52+
for(int i=0;i<mini;i++)
53+
{
54+
ans+=s[0][i];
55+
}
56+
return ans;
57+
}
58+
};
59+
60+

‎Palindrome_String.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,33 @@ int isPalindrome(string s)
3232
}else
3333
return 1;
3434
}
35+
36+
37+
//**************************************** Using Recursion********************
38+
39+
#include <iostream>
40+
41+
using namespace std;
42+
43+
bool f(int i,int n,string &s)
44+
{
45+
if(i>=n/2)
46+
{
47+
return true;
48+
}
49+
if(s[i]!=s[s.size()-i-1])
50+
{
51+
return false;
52+
}
53+
return f(i+1,n,s);
54+
}
55+
56+
int main()
57+
{
58+
string s="ababa";
59+
int n=5;
60+
int i=0;
61+
cout<<f(i,n,s);
62+
63+
return 0;
64+
}

‎Shuffle_string.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* https://leetcode.com/problems/shuffle-string/ */
2+
3+
4+
class Solution {
5+
public:
6+
string restoreString(string s, vector<int>& indices) {
7+
int n=s.length();
8+
char ans[n];
9+
for(int i=0;i<indices.size();i++)
10+
{
11+
int occ=indices[i];
12+
ans[occ]=s[i];
13+
}
14+
string t;
15+
for(int i=0;i<s.length();i++)
16+
{
17+
t=t+ans[i];
18+
}
19+
return t;
20+
}
21+
};
22+
23+
24+
//t[index[i]]=s[i]; one step

‎aa.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/******************************************************************************
2+
3+
Online C++ Compiler.
4+
Code, Compile, Run and Debug C++ program online.
5+
Write your code in this editor and press "Run" button to compile and execute it.
6+
7+
*******************************************************************************/
8+
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
int main()
14+
{ long long int num;
15+
int count1=0,count2=0;
16+
cin >> num;
17+
while(num!=1)
18+
{ if(num%2!=0)
19+
{
20+
num=(num*3) + 1;
21+
cout<<num<<" ";
22+
count1++;
23+
}
24+
else{
25+
num=num/2;
26+
cout<<num<<" ";
27+
count2++;
28+
}
29+
30+
31+
}
32+
cout<<" count total "<< count1+count2<< endl;
33+
34+
35+
return 0;
36+
}

‎aa.exe‎

51.2 KB
Binary file not shown.

‎anagram_string_or_not.cpp‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/*
33
link:- https://practice.geeksforgeeks.org/problems/anagram-1587115620/1
4-
4+
https://leetcode.com/problems/valid-anagram/
55
problem: Given two strings a and b consisting of lowercase characters.
66
The task is to check whether two given strings are an anagram of each other or not.
77
An anagram of a string is another string that contains the same characters,
@@ -24,3 +24,23 @@ class Solution
2424
return a==b;
2525
}
2626
};
27+
28+
/* using map*/
29+
30+
class Solution {
31+
public:
32+
bool isAnagram(string s, string t) {
33+
map<char,int>m1;
34+
map<char,int>m2;
35+
for(int i=0;i<s.length();i++)
36+
{
37+
m1[s[i]]++;
38+
}
39+
for(int i=0;i<t.length();i++)
40+
{
41+
m2[t[i]]++;
42+
}
43+
44+
return(m1==m2);
45+
}
46+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/*/
2+
3+
4+
class Solution {
5+
public:
6+
bool areOccurrencesEqual(string s) {
7+
unordered_map<int,int>m;
8+
for(int i=0;i<s.length();i++)
9+
{
10+
m[s[i]]++;
11+
}
12+
vector<int>v;
13+
for(auto i=m.begin();i!=m.end();i++)
14+
{
15+
v.push_back(i->second);
16+
}
17+
for(int i=0;i<v.size();i++)
18+
{
19+
if(v[i]!=v[0])
20+
{
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/*
3+
link:- https://practice.geeksforgeeks.org/problems/check-if-strings-are-rotations-of-each-other-or-not-1587115620/1
4+
5+
Given two strings s1 and s2. The task is to check if s2 is a rotated version of the string s1.
6+
The characters in the strings are in lowercase.
7+
8+
Input:
9+
geeksforgeeks
10+
forgeeksgeeks
11+
Output:
12+
1
13+
14+
Explanation: s1 is geeksforgeeks, s2 is
15+
forgeeksgeeks. Clearly, s2 is a rotated
16+
version of s1 as s2 can be obtained by
17+
left-rotating s1 by 5 units.
18+
19+
20+
Example 2:
21+
22+
Input:
23+
mightandmagic
24+
andmagicmigth
25+
Output:
26+
0
27+
Explanation: Here with any amount of
28+
rotation s2 can't be obtained by s1.
29+
30+
31+
*/
32+
33+
34+
// approch:- do s1+s1 and find window of s2 in s1 and check if s2 is there or not.
35+
class Solution
36+
{
37+
public:
38+
//Function to check if two strings are rotations of each other or not.
39+
bool areRotations(string s1,string s2)
40+
{
41+
if(s1.length()!=s2.length())
42+
{
43+
return 0;
44+
}
45+
else
46+
{
47+
string s=s1+s1;
48+
if(s.find(s2)!=string::npos)
49+
return 1;
50+
else
51+
return 0;
52+
}
53+
}
54+
};

‎delete_column.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
https://leetcode.com/problems/delete-columns-to-make-sorted/description/
4+
*/
5+
6+
class Solution {
7+
public:
8+
int minDeletionSize(vector<string>& str) {
9+
int row=str.size();
10+
int column=str[0].size();
11+
int count=0;
12+
13+
for (int j = 0; j < column; j++) {
14+
for (int i = 0; i < row-1; i++) {
15+
if(str[i][j]>str[i+1][j])
16+
{
17+
count++;
18+
break;
19+
}
20+
}
21+
}
22+
23+
24+
return count;
25+
}
26+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /