5 Strings
5 Strings
while(cin>>n){
}
String tokenization
int main()
{
tokenizing a string denotes
char str[] = "hi-i-am-AMAN";
splitting a string with respect to
char *token = strtok(str, "-");
some delimiter(s).
while (token != NULL)
{
cout<<token<<”\n”;
token = strtok(NULL, "-");
}return 0; }
Palindrome
Q->Check whether a given word is palindrome or not?
Reverse a string
Code sample for two pointers
#include<bits/stdc++.h>
using namespace std; SC-> O(1)
bool isPalindrome(string s){
int i=0,j=s.size()-1;
while(i<j){ TC->O(n)
if(s[i]!=s[j])return false;
i++,j--;
}
r a d a r
return true;
}
Using Reversing a string
SC-> just extra variable
string s;
for temp string
cin>>s;
int len; Time complexity-
cin>>len; >O(N)
string temp=s;
reverse(s.begin(),s.end());
for(int i=0;i<len;i++){
if(s[i]!=temp[i])return false;
}
return true;
Conversion techniques
“1234” is ?
requirements->
1. if numbers are given as char and
we need to do some operations
on it then we need to convert that
string into numbers
Methods for converting char to int /vice versa
STOI Atoi
String s=”12345”;
Int val=atoi(s);
Int x=stoi(s);
Practice Time
● https://leetcode.com/problems/palindrome-number/
● https://practice.geeksforgeeks.org/problems/sum-of-nu
mbers-or-number1219/1\
bool isPalindrome(int n) {
string temp; Q->Given a number
if(n<0)return false;
while(n>0){
check whether it is
temp.push_back(n%10+'0'); palindrome or not
n/=10;
TC-> O(n);
}
int st=0,en=temp.size()-1;
while(st<en){
}
if(temp[st]!=temp[en])return false;
st++,en--; 18581
return true;
}
string findSum(string x, string y) {
int i=x.size()-1,j=y.size()-1;
string res="";
Add two numbers
int cur=0,car=0;
while(i>=0 and j>=0){
cur=(x[i]-'0')+(y[j]-'0')+car; TC -> O(n)
if(cur>9){
cur=cur%10; 2 3 7
car=1;
}
4 9
else if(cur<10)car=0;
res.push_back(cur+'0');
i--,j--;}
while(i>=0){
int cur=(x[i]-'0')+car;
!!!What if some number is
if(cur>9){ still left ,means one among
car=1; two pointers is greater than
cur=cur%10; zero
}
else if(cur<10){
car=0;
}
res.push_back(cur+'0');
i--;
}
while(j>=0){
int cur=(y[j]-'0')+car;
if(cur>9){
car=1;
cur=cur%10;
}
else if(cur<10){
car=0;
}
res.push_back(cur+'0');
j--;
}
if(car>0){
What if carry is still in
res.push_back(1+'0');
existence ,
} Or greater than 1
while(res.back()-'0'==0 and res.size()>1){ ex-> 99+1
res.pop_back();
} Ahhh these leading
zeroes!!!!!!!!!!!
reverse(res.begin(),res.end());
return res;
}
Append two strings
● https://codeforces.com/problemset/problem/1674/B
}
RBS (regular bracket sequence)
Examples -
(1+2)-(4-3)
((1+2)*3)+(4)-3)
RBS /regular bracket sequence
•Number of closing brackets needed to complete a regular
bracket sequence
Output : (()(()()))
Output : IMPOSSIBLE
problem
1. https://codeforces.com/problemset/problem/26/B
ex->(()))(
Output ->4
void solve(){
string s; solution
cin>>s;
int n=s.size(); Time complexity ->
int open=0,pair=0; O(n)
for(int i=0;i<n;i++){
if(s[i]=='(')open++;
if(s[i]==')' and open>0)open--,pair++;
else if(s[i]==')' and open==0){
continue;
}
}
cout<<2*pair<<"\n"; return;}
Some useful functions
s.substr(ind,size)
isupper(str[i])
islower(str[i])
isdigit(str[i])
isalpha(str[i])