Skip to content

Commit ed65ade

Browse files
committed
/
1 parent 29dd40c commit ed65ade

15 files changed

+359
-17
lines changed

Add_minimum-Char_palindrome.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
/* https://practice.geeksforgeeks.org/problems/55d */
4+
5+
6+
int addMinChar(string str){
7+
//code here
8+
int n=str.length();
9+
int i=0;
10+
int j=n-1;
11+
int ans=0;
12+
while(i<=j)
13+
{
14+
if(str[i]==str[j])
15+
{
16+
i++;
17+
j--;
18+
}
19+
else
20+
{
21+
ans++;
22+
i=0;
23+
j=n-1-ans;
24+
}
25+
}
26+
27+
28+
return ans;
29+
}

Count_Number_of_consistent_string.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* https://leetcode.com/problems/count-the-number-of-consistent-strings/description/ */
2+
3+
class Solution {
4+
public:
5+
int countConsistentStrings(string allowed, vector<string>& words) {
6+
7+
int count=0;
8+
for(int i=0;i<words.size();i++)
9+
{
10+
for(int j=0;j<words[i].size();j++)
11+
{
12+
if(allowed.find(words[i][j])!=string::npos)
13+
{
14+
15+
}
16+
else
17+
{
18+
count++;
19+
break;
20+
}
21+
}
22+
}
23+
return words.size()-count;
24+
}
25+
};

Integer_To_Roman.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ class Solution{
1717
public:
1818
string convertToRoman(int n) {
1919
// code here
20-
int num[]={1,4,5,9,10,40,50,90,100,400,500,900,1000};
21-
string s[]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
22-
string ans;
23-
int i=12;
24-
while(n>0)
25-
{
26-
int div=n/num[i];
27-
n=n%num[i];
28-
while(div--)
29-
{
30-
ans=ans+s[i];
31-
}
32-
i--;
33-
}
34-
return ans;
20+
vector<string> notations = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
21+
vector<int> value = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
22+
string s;
23+
int i=0;
24+
while(num!=0)
25+
{
26+
if(num>=value[i])
27+
{
28+
s=s+notations[i];
29+
num=num-value[i];
30+
}
31+
else{
32+
i++;
33+
}
34+
}
35+
return s;
3536
}
3637
};

Longest_Balance_Binary_String.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
/* https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/*/
3+
class Solution {
4+
public:
5+
int findTheLongestBalancedSubstring(string s) {
6+
int res=0;
7+
string temp="01";
8+
while(temp.size()<=s.size())
9+
{
10+
if(s.find(temp)!=string::npos){
11+
res=temp.size();
12+
}
13+
temp='0'+temp+'1';
14+
}
15+
return res;
16+
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*https://leetcode.com/problems/longest-substring-without-repeating-characters/description/*/
2+
3+
4+
5+
class Solution {
6+
public:
7+
int lengthOfLongestSubstring(string s) {
8+
unordered_set<char>set;
9+
int i=0,j=0,n=s.length();
10+
int maxi=0;
11+
12+
while(i<n && j<n)
13+
{
14+
if(set.find(s[j])==set.end())
15+
{
16+
set.insert(s[j]);
17+
j++;
18+
maxi=max(maxi,j-i);
19+
20+
}
21+
else
22+
{
23+
set.erase(s[i]);
24+
i++;
25+
}
26+
}
27+
28+
return maxi;
29+
}
30+
31+
};

Maximum_Length.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* https://practice.geeksforgeeks.org/problems/84963d7b5b84aa24f7807d86e672d0f97f41a4b5/1 */
2+
3+
4+
class Solution {
5+
public:
6+
int solve(int a, int b, int c) {
7+
// code here || (b >(a+c)(a+c+1)/2) || (a>(b+c)(b+c+1)/2))
8+
if( (c>2*(a+b)) || (b >2*(a+c)) || (a>2*(b+c)))
9+
{
10+
return -1;
11+
}
12+
else
13+
{
14+
return a+b+c;
15+
}
16+
17+
}
18+
};

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
- transform(s.begin(), s.end(), s.begin(), ::tolower);
2626
- transform(s.begin(), s.end(), s.begin(), ::toupper);
2727

28-
- Find:- s1.find(s2[i]):- it means <find s2[i] in s1>
28+
- Find:- s1.find(s2[i])!=string::npos:- it means <find s2[i] in s1> is present
29+
30+
- token:- stringstream
31+
ex: string s = "geeks for geeks";
32+
stringstream ss(s); // Used for breaking words
33+
string word; // to store individual words
34+
while (ss >> word)
35+
cout << word << endl;
36+
37+
2938

3039

Remove_Stars_From_String.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* https://leetcode.com/problems/removing-stars-from-a-string/description/ */
2+
3+
class Solution {
4+
public:
5+
string removeStars(string s) {
6+
stack<char>st;
7+
for(char c:s)
8+
{
9+
10+
if(!st.empty() && c=='*')
11+
{
12+
st.pop();
13+
}
14+
else
15+
{
16+
st.push(c);
17+
}
18+
19+
}
20+
string ans;
21+
while(!st.empty())
22+
{
23+
ans+=st.top();
24+
st.pop();
25+
}
26+
reverse(ans.begin(),ans.end());
27+
28+
return ans;
29+
}
30+
};

Reverse_Word_in_String.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,26 @@ mno.pqr
5656
ans1=ans1+i;
5757
}
5858
return ans1+t;
59-
}
59+
}
60+
61+
62+
// https://leetcode.com/problems/reverse-words-in-a-string
63+
64+
65+
string reverseWords(string s) {
66+
stringstream ss(s);
67+
string word;
68+
vector<string>v;
69+
while(ss>>word)
70+
{
71+
v.push_back(word);
72+
v.push_back(" ");
73+
}
74+
v.pop_back();
75+
reverse(v.begin(),v.end());
76+
string ans;
77+
for(auto i:v)
78+
{
79+
ans=ans+i;
80+
}
81+
return ans;

Roman_To_Integer.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* https://leetcode.com/problems/roman-to-integer/ */
2+
3+
4+
class Solution {
5+
public:
6+
int romanToInt(string s) {
7+
8+
9+
unordered_map<char,int>m;
10+
m['I']=1;
11+
m['V']=5;
12+
m['X']=10;
13+
m['L']=50;
14+
m['C']=100;
15+
m['D']=500;
16+
m['M']=1000;
17+
18+
19+
int ans=0;
20+
for(int i=0;i<s.length();i++)
21+
{
22+
if(m[s[i]]>=m[s[i+1]] )
23+
{
24+
ans=ans+m[s[i]];
25+
}
26+
else{
27+
ans=ans-m[s[i]];
28+
}
29+
}
30+
return ans;
31+
32+
}
33+
};

find_divisibility_array_of_string.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*https://leetcode.com/contest/weekly-contest-334/problems/find-the-divisibility-array-of-a-string/*/
2+
3+
4+
class Solution {
5+
public:
6+
vector<int> divisibilityArray(string word, int m) {
7+
vector<int>v;
8+
string s;
9+
unsigned long long int remainder=0;
10+
11+
for(int i=0;i<word.size();i++)
12+
{
13+
int digit=word[i]-'0';
14+
15+
remainder=(remainder*10 + digit)%m;
16+
// int n=stoi(s);
17+
if(remainder==0)
18+
{
19+
20+
v.push_back(1);
21+
}
22+
else
23+
{
24+
v.push_back(0);
25+
}
26+
27+
}
28+
return v;
29+
}
30+
};

find_occurence_of_s2_in_s1.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
link:- https://practice.geeksforgeeks.org/problems/implement-strstr/1
3+
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
34
45
Input:
56
s = GeeksForGeeks, x = Fr
@@ -10,6 +11,24 @@ string GeeksForGeeks as substring.
1011
1112
*/
1213

14+
//NAIVE
15+
int strStr(string a, string b) {
16+
if(b.length()>a.length()) return -1;
17+
for(int i=0;i<=a.length()-b.length();i++)
18+
{
19+
20+
for(int j=0;j<b.length();j++)
21+
{
22+
if(a[i+j]!=b[j])
23+
{
24+
break;
25+
}
26+
if(j==b.length()-1) return i;
27+
}
28+
}
29+
return -1;
30+
}
31+
1332

1433
//Function to locate the occurrence of the string x in the string s.
1534
int strstr(string s, string x)

first-unique-character-in-string.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
/* https://leetcode.com/problems/first-unique-character-in-a-string/ */
3+
4+
class Solution {
5+
public:
6+
int firstUniqChar(string s) {
7+
map<char,int>m;
8+
for(int i=0;i<s.length();i++)
9+
{
10+
m[s[i]]++;
11+
}
12+
for(auto i=0;i<s.length();i++)
13+
{
14+
if(m[s[i]]==1)
15+
{
16+
return i;
17+
}
18+
}
19+
return -1;
20+
}
21+
};

permutation_of_string.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1?page=4&sortBy=submissions */
2+
3+
4+
vector<string>find_permutation(string S)
5+
{
6+
vector<string>v;
7+
sort(S.begin(),S.end());
8+
do{
9+
v.push_back(S);
10+
}
11+
while(next_permutation(S.begin(),S.end()));
12+
13+
return v;
14+
}

0 commit comments

Comments
 (0)