Skip to content

Commit 0d5b6ea

Browse files
committed
21/10
1 parent 068c8a6 commit 0d5b6ea

6 files changed

+197
-29
lines changed

Remove_Character.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Link:- https://practice.geeksforgeeks.org/problems/remove-character3815/1
3+
Problem:- Given two strings string1 and string2, remove those characters from first string(string1) which are present in second string(string2). Both the strings are different and contain only lowercase characters.
4+
5+
Input:
6+
string1 = "computer"
7+
string2 = "cat"
8+
Output: "ompuer"
9+
Explanation: After removing characters(c, a, t)
10+
from string1 we get "ompuer".
11+
12+
Input:
13+
string1 = "occurrence"
14+
string2 = "car"
15+
Output: "ouene"
16+
Explanation: After removing characters
17+
(c, a, r) from string1 we get "ouene".
18+
19+
*/
20+
string removeChars(string s1, string s2) {
21+
// code here
22+
string ans="";
23+
24+
for(int i=0;i<s1.length();i++)
25+
{
26+
// npos -> not found
27+
if(s2.find(s1[i])==string::npos)
28+
{
29+
ans=ans+s1[i];
30+
}
31+
}
32+
return ans;
33+
}

StrStr.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
Link:- https://practice.geeksforgeeks.org/problems/implement-strstr/1
4+
5+
Problem:- Your task is to implement the function strstr. The function takes two strings as arguments (s,x) and locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x in s (0 based indexing).
6+
7+
Note: You are not allowed to use inbuilt function.
8+
9+
Input:
10+
s = GeeksForGeeks, x = Fr
11+
Output: -1
12+
Explanation: Fr is not present in the
13+
string GeeksForGeeks as substring.
14+
15+
Input:
16+
s = GeeksForGeeks, x = For
17+
Output: 5
18+
Explanation: For is present as substring
19+
in GeeksForGeeks from index 5 (0 based
20+
indexing).
21+
22+
*/
23+
24+
/****** Using String Matching Naive Algo */
25+
if(x.length()>s.length())
26+
{
27+
return -1;
28+
}
29+
30+
for(int i=0;i<=s.length()-x.length();i++)
31+
{
32+
for(int j=0;j<x.length();j++)
33+
{
34+
if(s[i+j]!=x[j])
35+
{
36+
break;
37+
}
38+
else if(j==x.length()-1)
39+
{
40+
return i;
41+
}
42+
43+
}
44+
}
45+
return -1;
46+
47+
48+
49+
// ******** Using inbuild Function find() ********
50+
51+
// The function returns the index of the first occurrence of sub-string, if the sub-string is not found it returns string::npos
52+
if(s.find(x)!=string::npos)
53+
{
54+
return s.find(x);
55+
}
56+
else
57+
{
58+
return -1;
59+
}
60+

check_for_SUBSEQUENCE.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
problem:- Given two strings A and B, find if A is a subsequence of B.
4+
5+
Link:- https://practice.geeksforgeeks.org/problems/check-for-subsequence4930/1
6+
7+
8+
Input:
9+
A = AXY
10+
B = YADXCP
11+
Output: 0
12+
Explanation: A is not a subsequence of B
13+
as 'Y' appears before 'A'.
14+
15+
Input:
16+
A = gksrek
17+
B = geeksforgeeks
18+
Output: 1
19+
Explanation: A is a subsequence of B.
20+
21+
*/
22+
23+
bool isSubSequence(string s1, string s2)
24+
{
25+
// code here
26+
int i,j=0;
27+
while(i<s1.length() && j<s2.length())
28+
{
29+
if(s1[i]==s2[j])
30+
{
31+
i++;
32+
}
33+
j++;
34+
}
35+
if(i==s1.length())
36+
{
37+
return 1;
38+
}
39+
else
40+
{
41+
return 0;
42+
}
43+
}

find_occurence_of_s2_in_s1.cpp

-28
This file was deleted.

maximum_occuring_char.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
3+
Link:- https://practice.geeksforgeeks.org/problems/maximum-occuring-character-1587115620/1
4+
5+
problem:- Given a string str. The task is to find the maximum occurring character in the string str. If more than one character occurs the maximum number of time then print the lexicographically smaller character.
6+
7+
Input:
8+
str = testsample
9+
Output: e
10+
Explanation: e is the character which
11+
is having the highest frequency.
12+
13+
Input:
14+
str = output
15+
Output: t
16+
Explanation: t and u are the characters
17+
with the same frequency, but t is
18+
lexicographically smaller.
19+
20+
*/
21+
22+
char getMaxOccuringChar(string str)
23+
{
24+
// Your code here
25+
map<char,int>m;
26+
for(int i=0;i<str.length();i++)
27+
{
28+
m[str[i]]++;
29+
}
30+
int max=0;
31+
char c;
32+
for(auto i=m.begin();i!=m.end();i++)
33+
{
34+
if(max<i->second)
35+
{
36+
max=i->second;
37+
c=i->first;
38+
}
39+
}
40+
return c;
41+
}

repeated_char.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ class Solution
3636
}
3737
return '#'; // '#' = -1 in ascii
3838
}
39-
};
39+
};
40+
41+
/*
42+
if given find FIRST repeated character
43+
44+
string ans="";
45+
unordered_map<char,int>m;
46+
for(int i=0;i<s.length();i++)
47+
{
48+
m[s[i]]++;
49+
50+
if(m[s[i]]==2)
51+
{
52+
ans=ans+s[i];
53+
return ans;
54+
}
55+
}
56+
57+
58+
*/

0 commit comments

Comments
 (0)