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 4f40051

Browse files
my commit
0 parents commit 4f40051

15 files changed

+696
-0
lines changed

‎Binary_String.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
link:- https://practice.geeksforgeeks.org/problems/binary-string-1587115620/1
3+
problem:
4+
Given a binary string S. The task is to count the number of substrings that start and end with 1.
5+
For example, if the input string is "00100101", then there are three substrings "1001", "100101" and "101".
6+
7+
Input:
8+
N = 4
9+
S = 1111
10+
Output: 6
11+
Explanation: There are 6 substrings from
12+
the given string. They are 11, 11, 11,
13+
111, 111, 1111.
14+
15+
Input:
16+
N = 5
17+
S = 01101
18+
Output: 3
19+
Explanation: There 3 substrings from the
20+
given string. They are 11, 101, 1101.
21+
22+
*/
23+
24+
class Solution
25+
{
26+
public:
27+
//Function to count the number of substrings that start and end with 1.
28+
long binarySubstring(int n, string a){
29+
30+
// n = length of string , a = binary string
31+
int ans=0;
32+
sort(a.begin(),a.end());
33+
for(int i=0;i<n;i++)
34+
{
35+
if(a[i]!='0')
36+
{
37+
ans++;
38+
}
39+
//ans= count(a.begin(), a.end(), '1');
40+
}
41+
// return count;
42+
return (ans*(ans-1))/2;
43+
}
44+
45+
};
46+
47+
48+
/*
49+
50+
approch:-
51+
52+
1.count the total number of 1's in the string ( using sort string and for loop)
53+
2. observe pattern if
54+
number of 1 is 1 then ans 1
55+
number of 1 is 2 then ans 2
56+
number of 1 is 3 then ans 3
57+
number of 1 is 4 then ans 6
58+
number of 1 is 5 then ans 10
59+
.
60+
.
61+
number of 1 is n then ans n*(n-1)/2
62+
63+
*/
64+

‎Integer_To_Roman.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
link:- https://practice.geeksforgeeks.org/problems/convert-to-roman-no/1
3+
4+
problem:- covert integer to roman number
5+
6+
Input:
7+
n = 5
8+
Output: V
9+
10+
Input:
11+
n = 3
12+
Output: III
13+
14+
*/
15+
16+
class Solution{
17+
public:
18+
string convertToRoman(int n) {
19+
// 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;
35+
}
36+
};

‎anagram_make.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
3+
/*
4+
problem link:- https://practice.geeksforgeeks.org/problems/anagram-of-string/1
5+
6+
problem statement:- Given two strings S1 and S2 in lowercase, the task is to make them anagram.
7+
The only allowed operation is to remove a character from any string.
8+
Find the minimum number of characters to be deleted to make both the strings anagram.
9+
Two strings are called anagram of each other if one of them can be converted into another by rearranging its letters.
10+
11+
Example 1:
12+
13+
Input:
14+
S1 = bcadeh
15+
S2 = hea
16+
Output: 3
17+
Explanation: We need to remove b, c
18+
and d from S1.
19+
20+
Example 2:
21+
22+
Input:
23+
S1 = cddgk
24+
S2 = gcd
25+
Output: 2
26+
Explanation: We need to remove d and
27+
k from S1.
28+
29+
*/
30+
31+
int remAnagram(string str1, string str2)
32+
{
33+
34+
int m1[26]={0},m2[26]={0},res=0;
35+
for(int i=0;i<str1.length();i++)
36+
{
37+
m1[str1[i]-'a']++;
38+
}
39+
for(int i=0;i<str2.length();i++)
40+
{
41+
m2[str2[i]-'a']++;
42+
}
43+
for(int i=0;i<26;i++)
44+
{
45+
res=res+abs(m1[i]-m2[i]);
46+
}
47+
return res;
48+
}
49+
50+
//Dry run
51+
/*
52+
53+
s1=cddgk
54+
s2=gcd
55+
56+
s1 map | s2 map | diff | res
57+
c-1 | c-1 | 0 | 0
58+
d-2 | d-1 | 1 | 1
59+
g-1 | g-1 | 0 | 0
60+
k-1 | | 1 | 1+1=2
61+
62+
res=output=2
63+
64+
*/

‎anagram_string_or_not.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
link:- https://practice.geeksforgeeks.org/problems/anagram-1587115620/1
4+
5+
problem: Given two strings a and b consisting of lowercase characters.
6+
The task is to check whether two given strings are an anagram of each other or not.
7+
An anagram of a string is another string that contains the same characters,
8+
only the order of characters can be different.
9+
For example, act and tac are an anagram of each other.
10+
11+
12+
Input: a = geeksforgeeks, b = forgeeksgeeks
13+
Output: YES
14+
Explanation: Both the string have same characters with
15+
same frequency. So, both are anagrams.
16+
*/
17+
18+
class Solution
19+
{
20+
public:
21+
bool isAnagram(string a, string b){
22+
sort(a.begin(),a.end());
23+
sort(b.begin(),b.end());
24+
return a==b;
25+
}
26+
};

‎array_subset_of_another_sum.cpp‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
link:- https://practice.geeksforgeeks.org/problems/array-subset-of-another-array2317/1
3+
4+
problem:-
5+
Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m. Task is to
6+
check whether a2[] is a subset of a1[] or not.
7+
Both the arrays can be sorted or unsorted.
8+
9+
Input:
10+
a1[] = {11, 1, 13, 21, 3, 7}
11+
a2[] = {11, 3, 7, 1}
12+
Output:
13+
Yes
14+
Explanation:
15+
a2[] is a subset of a1[]
16+
17+
Input:
18+
a1[] = {10, 5, 2, 23, 19}
19+
a2[] = {19, 5, 3}
20+
Output:
21+
No
22+
Explanation:
23+
a2[] is not a subset of a1[]
24+
25+
*/
26+
27+
string isSubset(int a1[], int a2[], int n, int m)
28+
{
29+
30+
unordered_set<int> s(a1, a1 + n);
31+
for (int i = 0; i < n; i++)
32+
{
33+
s.insert(a1[i]);
34+
}
35+
int cnt = 0;
36+
// cout<< s.size();
37+
for (int i = 0; i < m; i++)
38+
{
39+
if (s.count(a2[i]))
40+
{
41+
cnt++;
42+
}
43+
if (cnt == m)
44+
{
45+
return "Yes";
46+
}
47+
}
48+
return "No";
49+
}
50+
51+
/*
52+
53+
OTHER APPROCH:-
54+
55+
string isSubset(int a1[], int a2[], int n, int m) {
56+
57+
vector<int>v1(a1,a1+n);
58+
vector<int>v2(a2,a2+m);
59+
int f=0;
60+
vector<int>::iterator it;
61+
int i=0;
62+
while(i!=v2.size())
63+
{
64+
it=find(v1.begin(),v1.end(),v2[i]);
65+
i++;
66+
if(it!=v1.end())
67+
{
68+
69+
}
70+
else
71+
{
72+
f=1;
73+
}
74+
}
75+
if(f==1)
76+
{
77+
return "No";
78+
}
79+
else
80+
{
81+
return "Yes";
82+
}
83+
}
84+
85+
*/
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+
};

‎find_occurence_of_s2_in_s1.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
link:- https://practice.geeksforgeeks.org/problems/implement-strstr/1
3+
4+
Input:
5+
s = GeeksForGeeks, x = Fr
6+
Output: -1
7+
8+
Explanation: Fr is not present in the
9+
string GeeksForGeeks as substring.
10+
11+
*/
12+
13+
14+
//Function to locate the occurrence of the string x in the string s.
15+
int strstr(string s, string x)
16+
{
17+
//Your code here
18+
// char p=strstr(s,x);
19+
if(s.find(x)!=string::npos)
20+
{
21+
return s.find(x); //returns the index of the first occurence of x in s
22+
}
23+
else
24+
{
25+
return -1;
26+
}
27+
28+
}

0 commit comments

Comments
(0)

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