DSA Sheet by Arsh1
DSA Sheet by Arsh1
shivani patel
//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>
}
}
int main()
{
string s="shivanishivi";
dupl(s);
return 0;
}
3. Easy Level- Implement strStr()
Code:
#include <bits/stdc++.h>
#include <iostream>
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>
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>
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;
shivani patel
SHIVANI PATEL 5