0% found this document useful (0 votes)
28 views5 pages

DSA Sheet by Arsh1

Uploaded by

starktonny762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

DSA Sheet by Arsh1

Uploaded by

starktonny762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DSA Sheet By Arsh

Solution Of String Easy Level Problem

shivani patel

SNo. PROBLEM STATEMENT


1. Easy Level-Valid Parentheses
Code:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;


bool isValid(string s)
{
stack<int>st;

//stack st;
for (int i=0;i<s.size();i++){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
st.push(s[i]);
}
else{
if(st.size()==0) return false;
if(s[i]==')'&& st.top()=='('||s[i]=='}'&&
st.top()=='{'||s[i]==']'&&
st.top()=='['){
st.pop();
}
else {return false;}
}
}
if(st.size()==0) {return true;}
return false;
}
int main()
{
string s="()[]{}";
if(isValid(s))
cout<<"Valid";
else
cout<<"Not valid";

return 0;
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution Of String Easy Level Problem

shivani patel

}
2. Easy Level-Print all the duplicates in the input string.
Code:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;


void dupl(string s)
{
unordered_map<char,int>m;
for(int i=0;i<s.size();i++)
{
m[s[i]]++;
}
for(auto it:m)
{
if(it.second>1)
cout << it.first << ", count = " << it.second<< "\n";

}
}
int main()
{
string s="shivanishivi";
dupl(s);
return 0;
}
3. Easy Level- Implement strStr()
Code:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int impstr(string haystack, string needle)


{
if(haystack.size()==0 and needle.size()==0)
return 0;
return haystack.find(needle);
}
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution Of String Easy Level Problem

shivani patel

int main()
{
string haystack = "hello", needle = "ll";
int res = impstr(haystack, needle);
if (res == -1)
cout << "Not present";
else
cout << "Present at index " << res;
return 0;
}
4. Easy Level- Longest Common Prefix.
Code:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

string longestCommonPrefix(vector<string>& s)
{
if(s.size()==0)
return " ";
else
{
string s1=s[0];
for(int i=1;i<s.size();i++)
{
for(int j=0;j<s1.size();j++)
{
if(j==s[i].size() or s1[j]!=s[i][j])
{
s1=s1.substr(0,j);
break;
}
}
}
return s1;
}

}
int main()
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution Of String Easy Level Problem

shivani patel

{
vector<string>s={"flower","flow","flight"};
string res=longestCommonPrefix(s);
cout<<res;
return 0;
}
5. Easy Level- Valid Palindrome II
Code:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

bool check(int start,int end,string s)


{

while(start<end)
{

if(s[start]==s[end])
{
start++;
end--;
}
else
return false;
}
return true;
}
bool validPalindrome(string s) {

int start=0;
int end=s.length()-1;

while(s[start]==s[end] && start<end)


{
start++;
end--;
}
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution Of String Easy Level Problem

shivani patel

return check(start+1,end,s)|| check(start,end-1,s);


}
int main()
{
string s="mom";
int start=0;
int end=s.size()-1;
if(check(start,end,s))
cout<<"Palindrome";
else
cout<<"Not Palindrome";
return 0;
}

SHIVANI PATEL 5

You might also like