Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Strings

Programming Club - AKGEC


Major Topics
➢ Palindrome
➢ Conversion from char to integer and vice versa
➢ String append /plus
➢ Dictionary
➢ Regular bracket sequence
➢ Famous Questions
Input a string
● cin
● string array examples
● getline “ABCDEF”,
”12345”,
“abc123”
1. string s; cin>>s; (for mainly single string)
2. string arr[n] ; (when there are multiple
words )
3. getline(cin,str); (when you want to input
the complete line in one string)
Unknown number input

● cin provides formatted stream extraction. The


operation cin >> x;where "x" is an int will fail if a non-
numeric value is entered. So:
Int n;

while(cin>>n){

//code to be executed every time

}
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?

Input Given-String s, int len Example


Output -true or false “radar”
Methods
5
Two pointers

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

subtract ‘0’ Add “0”


➔ char to int Int to char to_string()
➔ string c=”9”; int x=9; Int val=123;
➔ int x=c-’0’; string c=x+’0’; String s=to_string(val);
➔ x=9; c=”9”

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

● Concatenate two strings,appending two strings


● Use ‘+’ to do all these stuffs
● Input ->String a, string b
● String c=a+b;

abc def abcdef


Dictionary -practice question

● https://codeforces.com/problemset/problem/1674/B

Q-> Tell the position of the word in the


dictionary,
Words are made up of only two distinct
letters
ex->ab,de,zs
void solve(){
string s;
Solution
cin>>s;
ll ans=(s[0]-'a')*25+(s[1]-'a');
if(s[1]<s[0]){
ans++;
}
TC->O(1)
cout<<ans<<"\n";

}
RBS (regular bracket sequence)

● When we play around the brackets and its sequencing,


● It should be arithmetically valid

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

•Input : str = “(()(()(”

Output : (()(()()))

Explanation : The minimum number of ) needed to make the


sequence regular are 3 which are appended at the end.

•Input : str = “())(()”

Output : IMPOSSIBLE
problem
1. https://codeforces.com/problemset/problem/26/B

Q->max length of regular bracket sequence


that can be obtained from the string by deleting
some brackets

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])

You might also like