0% found this document useful (0 votes)
24 views73 pages

Strings

Uploaded by

naneet yadav
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)
24 views73 pages

Strings

Uploaded by

naneet yadav
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/ 73

()

!"#$%&' *
+

5$%3/5
'.-220'" 3$'"$%4"
-%-&#-.' "/&0"10#'89'
*
,#$%" -

20%&"1 "#$%& 6/%&0'"


,-2$%3#/.$4 !899$%&
6/%&0'"
070% :

"$.0
*

$% 2$%0-#
,/''$920 5/#3'
;#/. <1/%0 3$&$"' =-2$3 ,-#-% "1$' $'
6/%&0'"
*

>2$0%
?$4"$/%-#@ 4#8'1
B-%3@
*

A0'"#$4"$70
<-2$%3#/.$4 '899$%&
:

6/%&0'"
1$&10# ;/-2/; 3$&$"'8'$%&
*

%/
C0D"
?0'$&% '1/#"0%0# *
* EA6 '-.0 '0"

A070#'0 0-41
5/#3 $%
&$70% '"#$%& /;
"5/
-
!8. .8.'
2-#&0
*
:

F-#< '0-#41 <-""0#%


A-9$%
-&/ 4/28.% ;#/. &$70%
*

-
%-.0
41-#-4"0#'
:

A0-##-%&0 4/28.% %8.90#


*

G%"0#20-703 '"#$%&' >33


H$%-#@ '"#$%&'
*

-33#0''0'
:

G,
&0%0#-"0
;/#. <-2$%3#/.0
*
-

/; '"#$%&' #89'
:

B$#420
'$92$%&'
B/8%"
<-2$%3#/.$4
*

20%&"1 /; 2/%&0'" "$%@


*

IJG
/; '"#$%&
-

>
G.<20.0%" ;-4" /; 2-#&0
*
.8. K

-2&/ B/8%" #070#'-2'


*
!0-#41 ,-""0#% FL,
-

!0-#41 ,-""0#%
-&/ ,-2$%3#/.0 $%
)

2/%&0'"
*

*
-33#0''
=-2$3-"0
'"#$%&
*
-% G, -

%8.90#' $% F '5-<' ;/22/5$%& <-""0#%


6-#&0'" C8.
+

* :

MD402 '100"
/; &$70% '"#$%&
<-#" G K
*
,0#. 8"
L82"$<2@
"5/
'"#$%&'
:

/; 020.0%"'
20%&"1 F
!89'"#$%&'
B/8%"
'89'0N80%40' /; "@<0
:

*
5$"1 F +

2
K
-" /P
9
L-"41
O

A0<0-"03 '1$%@
!"#$%& ;/#.-"$/% ;#/. '89'"%%&
O
-

2/%&0'" "#@$% '@;$%


:

/; 3$'"$%4"
C8.90# 5/#3' 5$"1
*

P 7/502'
4/%"$&8/8'
.-D

-
<-2$%3#/.0"#-@
2/%&0'" '89' "/
;/#.
QQ))R)Q
;$%-2
<0#.8"-"$/%'
*

A-%P "10
> 1-' "/ 90
*

.$%$.8. "$.0'
H
#0<0-"03 $"
*
'0" $' -

59'"%%& /;
:
Sliding window

You need to record all the starting insides of


anagrams of string b in string and you need to
Return the vector.

We will us sliding window to solve this question

Time complexity: O(s


This question also uses the concept
Of sliding window.

We are creating an unordered map of strings as key and vector of strings


GFG HARD Microsoft

Topological sort application


S
S
$' 9280
I10
S T
'P@
U S

9280 $'
'P@
"10
S

$%;2/5 V
/ T
"1$' $%
4-% 3/
@/8
/8" '<-40 V
>'
@/8
3/%K" 1-70 0D"#-
'<-40 O

"10 51/20
'"#$%& ;$#'" /1 O "$.0
A070#'0 /8" '<-40
S I10 =
S
482" '$
&P'
01" T
9280 $'
'2-@
1$"' X
K" 0
90&$%
6/5 ;: 90&$%
' :

O
W #070#'0

'"#/%&
$" -" /% "10
$

:
Input : str = "geeksforgeeks" Output: 1 Input : str = "bbbbb" Output: 0
Explanation: Explanation :
All the repeated characters of the given string can be
rearranged so that no adjacent characters in the string is Repeated characters in the string cannot be rearranged such
equal. Any correct rearrangement will show a output of 1. that there should not be any adjacent repeated character.
First approach using PQ
blocks[4]={cccc->aacc->aabc} For all cnt in
cnts—> 214 k=1
I=0 sorted_cnt
cbcacbac —>a2b1c4 2a1b4c -> Sort -> 4c2a1b max_cnt=4
In the first solution we are using a function to
rearrange our strings and we are returning a string as
output. The input parameters are string s and the k
which tells, the minimum distance of a character with
it’s next occurrence if it occurs more than once.

We are creating a count array of all the 26 characters


and preinitialising the array with 0 and then with the
for loop we are updating the count of every character
in the count array.
Now we have a vector of pair of int key and char
value and we are naming it as sorted_cnts. It will
simply add all the characters in the vector from the
end in sorted order. We can also use push_back.

We will now sort the count vector in decreasing order


As we need the character who has highest count. So we
will store the max_count in a variable which is first of first
element of array. Then we will create a string blocks of
the max count length. And now for every character in
this sorted array, we will place the character in the
blocks array till its count is over. And then we will update
the index I as 1,2,3,0 as 4%4=0 then we check for 2a
cccc->aacc-> I=2 it didn’t become 0. At index 2 we will
push b and we get -> aabc, k=1
We return null if we don’t have enough characters
we add the characters into the result string
And for the last character we just add it and return result.

if k=0 then return str, it is the base case

Create an unordered map of freq of chars

create a maxheap and store every count and its char


In the heap as pair.

declare a string result and then till the heap is not empty
cnt= string length - result length = string length initially
as result=null string
Store the char at top of heap in cnt_char and pop that
top from heap.
push it in result and if the count after decrementingthe
top is greater than 0 place the char with the greater
count in other max heap and lets call it as used max
heap.
Move the character from heap 1 to heap2.
:
We are doing a BFS and we are doing so in the
interleaved fashion storing the two strings index
counts in the pair
And we are checking for the string A and String B that
if its corresponding index is matching, and then we
push the one more index corresponding to the
STRING and then if it matches in s3 the sum of first
and second then we push the corresponding index.

and mark visited true, and do all the checking if the


index is visited for the very first time and not before
that .
We are creating a Graph adjacency matrix of 26 cross 26 for all the
Alphabets and then we are performing DFS on the graph.
If the particular character is visited, leave it, if it is not visited, mark
It as visited if the value at the vertex indexed at I is set then visit
its neighbours and perform recursive DFS calls.

IsCircle is checking, whether you can form euler circuit in the


graph
Or not. We initialise the graph by 0 and then we create two vectors
indegree and out degree and pre initialise both of them by 0.
Then we traverse the string array given to us and then for all
strings
In this array we write a for loop and then we store the integer value
associated with the first and last index of every string in a and b
resp
Then we update in degree and out degree of both a and b by 1 and set the graph[a][b] as 1 this means we have stored it’s entry in
the graph. Now we will write one more for loop for every character, it will check, if at any point of time if the in degree of I is not
equal to the out degree of I, then we will return 0. So it is kind of a trap. Now we will make a visited vector of bool type.
For evert character if in degree of some character is not 1 then we will break at that point and do a DFS on that character and now
after this dis call gets completed, we will once again check ki if for any character there is. In degree but it is not yet visited we will
return false.

At the last, if everything is ok, we will return true.

Both are same questions one is GFG and one is Leetcode


What we are using is we are creating a longest proper prefix LPS and storing the
Frequency of repeating prefixes and if the pattern matches we are updating the length
and incrementing I and if it is not matching we are checking if it is the first occurence or
0 length then we are setting LS[I] as 0 and moving I one step further and otherwise if
The length is not 0 but pat[I] is not matching with pat[len] then in that case, we are
updating the length of the string as ls[len-1] i.e. previous value in the LS array

Now we are creating a KMP search function where we are passing string as well as the
Pattern in the function. Now we have stored their lengths and now, we will create a
Vector of integer type LS of size pattern length. i.e. n.
Call LPS(pat,LS) and then
Now start from j=0 and I=0. And make a vector ans where we will store the occurences
of the pattern in the string. Now we will traverse in the while loop from I=0 to m i.e. size
of the string. If the text at ith location is similar to pattern at jth location then in that case
You will increment both I and j

if your j has reached the pattern length then in that case, we will push i-j+1 in the answer
That would be the first instance of the pattern in the string as we need the beginning of
the pattern index which is I-j+1. Also please assign j=previous LS[j-1]
Else if I is less than m and string at index I is not matching with the pattern j. Then in that
Case you have to check if j=0 then you have to increment I and otherwise you will update
j as LS[j-1] Ie. Keep going back as you have already stored the repeated substring in the
Prefix array.
At the end return the answer.
#/90
'"$%&
</'$"$/%03
5-D
T T

U '"/#$%&
41-#-"
</'$"$/% </'

Z 8<3-"$%&
L-D$.
:

8<3-"0 P

T $;$" K'
'5[;@:.0
T #048#'$/%

'5-< -&-$%
K

What we are doing is using a backtracking based solution

-22/503
5$"1 Q(\) 20702
/; '5-<'
%/
+
]^(Q)
+
L-@
"
Y SS `

_]'(''P.M
S
S

;
](^Q)a" ^]OQQ)QQ Q(]^b)QOQ
c
d (]^Q) &
de

c
Numbers can be negetive

Positive Numbers only


Dry run
We have to rearrange every length of substring and check
Whether it is the longest palindromic subsequence or not
and we have to return the number of changes we have to
make in the string so that we are able to form the longest
palindromic substring.

This question is different from LPS because, here you do


not have to find the LPS, but you have arbitrary characters
given in the string, and you have to shuffle them in order to
make them a palindrome and then check that is this the
best LPS you have got after shuffling and store it in a
string answer and then keep on checking if you get any
longest palindromic substring after rearranging the
characters in the string offcourse.

Approach:
We have to keep on storing the characters and their
frequency at each given point of time in the string and then
try to place them in an order such that they become
palindrome.

Suppose my I and j pointers are such


i=0 and j=5 and the string that I have processed is
ababb then I am able to see that I have three b and 2 a
So what I will do is I will place the b in the middle and try
to place a on the left and right and I will do so if my a%2 is
0 to make an odd length palindrome.

aba
Can I use DP to solve this problem? Then I will see that we have two more B left so we will
SO suppose from I to j you have found babab as LPS till now and it is place then on the left and right babab now we can see that
Best having length 5, now suppose at 6 we have a character c so can Till now we have 5 length longest palindromic
we make it a palindrome again? Or the palindrome will be babab only subsequence.
So we are able to see a pattern here, if at a given point of time, the
character at middle of the previous result is same as the character at the new index j then we will insert that into the result by
Result = result.substr(0,mid+1)+s[j]+ result.substr(mid+1,j) then our string will become babbab

So storing previous result is looking to be useful. Now we will think that how to handle the odd length palindrome? So we see
Once we have formed a palindrome in the previous result, all we need to care is about if the resultant string was even length, then
no matter what just insert the upcoming character into middle as it will be placed in the centre, but if you have the string of odd
length in that case, you have to think what you can do.

adbabd

a a1
ad ->a, a1, d1
Adb—>a, a1, d1, b1
adba—>aa a2,b1,d1
Adbab-->baab a2,b2,d1
adbadb->dbaabd a2,b2,d2
Bitmasking Solution

Intuition:
KMP algorithm
Time, space : O(n) sliding window
Time: O(n)
Space: O(k)

stack
string
Pair class

Y
S
f^(^f
S

C \

/8"<8" T %0D"
<-2$%3#/.0
</''$920 8'$%&
'-.0
$;

'"#$%&
"10
41-#-4"0#'
/;

gMM
+
'/ O
U
"0.< :

h i
41-# O $%" j

8'0 -$8%$;030#03$ '"/#0

41-#
;#
:
/;

G'
K

kl
K

i2'K&Hd
020.0%"
/;
%0D"
$%

"10 4/8%"
4104P $" :

.-< $'

j G

mf(f^
"$
:

02'0 #0"8#% +

+
"
"1$' -<<#/-41
5/#P
5/%K"
/; 3$&$"'
S
i \f T #0"8#% G
Q)Q
+
S

% \
$; %/

-' %0D"
&#0-"0# ;-"0 5$"1
$' %/"
'-.0
3$&$"' </''$920 :

(]^Q^^nQo](
S
S

%\
$3 2 2
K

2
p '$%40 "10 %8.90#
!/
/% 90
$"
2-#&0
4-% K" ;$" $%

K K %"

3/
9@ _
%003 "/
/;
50
!/#"
'/.0

'"#$%& .-%$<82-"$/%
p
qX
K

>
O

$;: "<:IdN
$30- :

r a *
O
G

u
s # t '

!
!; (
+

&
( * /

I4& .$3

QX ;$%3 .$3 T
GG +

$v + Q\(
:

'$30 ' "


.$3
20;"
+ :

"#-70#'$%& ;#/.
T
)X '"-#"

w
+

q n !
50 '-5 @

p ]iq Y
G -'
"#-70#'$%& ;#/. & "/ '
K

fX C/5 '"-#" :

$: Ix "/ .$3
0

"1-% ]

3$&$"020.0%"
'.-220'"
'0-#41
&#00"0#
:

"10
$:
&#0-"0#
/%
0 %0D"

I1" #
j '.-220'"
S
+

y
S
^
S

#-%&0 &
:

$"K'
50 '00
n: Y -' nn]
\

020.0%"
$; %/ '841 $'
;/8%3
#0"8#% + 2 E

'.-220'"
+

p
N

'.-220'"X
'5-;4$&
+

'.-220'" X
'5-< r
G
:

% G $ %
+
+

O
+
+
@:g?$:3P .$3
O

#070#'0
020.0%"' $%

.$3X
#-%&0 Bx O

%003 "/
H -2'/ 50
$;
070%
%

#070#'0 :

.$3 x G v X
#-%&0r
% + +

&

02'0 $; % $'
/33 :

#07$'0

#-%&0 r .$3 ") O % +


, +

):X
/; <0#.8"-"$/%
0D"0%'$/% %0D"
I1$' N80'"$/% $' -%

1-70 %8.90#
!8<</'0 @/8
-

.z.zz. "#-70#'0
:$%

;#/.
"10
%
"1/"
+
:

W ;$%3 510%
(() { /448##0%40
n^(
{ ;$#'"
Y | 6-2$" |
])n -&/

-4" | 5$"1
"10 ;$#'"
%/5
'5-< "/ $" 2

;#/.
%
+
G
/448##0%40

/1%0
K

-4%0 - B>? u

#
f
@
)
#070#'0
` ) f ( ^]
G

R}n]]n} +
y Q^]n
~0.k?
-
I
.$3
:

 ] @ %
\
( .$3
+ )

R G ) f

g+
G
S S K

"$#0 G :
l
*
:

(]
]nn] Y

$
" :

] ]Q
@ @
S

-
S
nQ)]fq}QQ)(

:
"9S&&2)fqqqn Y

070%
$%"

#0'82" k
n
2/%& 2/%&
S 4-%
K"
$"
-4$"9 1-%320
$; + GG
4-##@
2/

41-#4#013"4$;$1"N4$v"P:?F
\?
$; r 4-#%@
+

S
S
#0'82" k &
"/ 1-%320

Z J

4-'0
Ä-2-

RRRR J J

J J
" J J
I"" 33 +

\
-%@ RR
RRu)f R

51$20 G/; +

9#00P' :
kadane's algo

binary search
u
Number following a pattern

#z."o"-3
T

T
<8'1 .8.
$%4#0.0%"
KGÅ??
+(f(
T
..
G
T G

Q)]^(fFÅ {'&;
T <$'1 !"-30%
T $%4#0.0%"

Ç
/8#' k S
0.
:

8<3-"0 3$& p
T
<<'10P1&

d
O

'"-4P
;.15.. :

We are taking a pattern here and the pattern contains only I and D’s and now our task is to print the output as
numbers.

If the input is IIDDD this means first two numbers are increasing and then next three numbers are decreasing, now
one challenge is that we need to use minimum numbers, so once you see I you can keep on printing 1 and then if
you see D then you have to push it into the stack. The idea of stack is that you will use minimum numbers and once
you reach the end of the input string then you start popping the elements from the stack and start pushing it in the
string end.

<@N4%8(&QÉ/%'."%@$.:"
IIDDD 3+'<8'1;%8.:""X G+'<#'1;85:""|
1
12 /1$"0'"-/P1.'21
Push 3 in stack
push 4 in stack
Push 5 in stack
Reached end?
pop string top and Add 5 to string—>125
Pop string top and add 4 onto the string end—> 1254
pop string top and add 3 onto the string end—> 12543
P-;.'".@$2 :

Now it was the intuition, if the input begins with D push 1 followed by 2 in stack and then keep on adding numbers if
you see other numbers in the input, then again do the same thing that you have to push the numbers

We are simply assigning num=1 and then doing case analysis if character is D then we do something else and if it
is the I then you again do the same thing that you push the number in the stack and then you simply increment
:

number.
Permutation of a given string

K
K

Q)f $2

f "$.0'
:

K
G

K )f
;02" Q)f
QQnQ)f K

V
Ñ#
GM V
Q)Q
+

+ %O

Create a vector of string type ans and then create two strings ip=S and a Snull string as op
;#
K
$K
S
Then call function f(ip, op, ans) K K
Q)
<
K +

' Q)
This function will do something.
Then we will sort the vector ans.
Now let us see what this function does
We are passing two strings ip and op into the function and passing the vector.
Then we are storing ip.length() in n. If n is 0 then we will simply push op into answer this
means we have passed the ans vector as reference, so we need not worry about anything
simply return, But If n>0 then in that case, we will run a for loop till the length of ip string.
Then we have two strings op1 and ip1 and we are copying op and ip into those and then
we are pushing ip[I] in op[I] and then we are erasing ip1 from start to ith index and then we
are calling f(ip1, op1,ans)
Longest palindrome in a string

T
&-<
[ p
&-< "/ % K

$zMzÅ
K

$"' B[ |
[

+
2
W G # ' "O "1$' G' n

kz"ÅzÅ;IÅdzz
+
:
G 4 1 - # - 1 - '

`fd;;;;Ig .-D3;
R]
+

k
z
K

dI;;;
Y
_

'
1 I ; ;
Å]
B
I ;
Substrings of length k with k-1 elements
Repeated string match

Rolling hash method


H$%-#@ !0-#41 ,3 I#00
'0&.0%"
'"-4P t8080
+

O O O
O

H$%%@ ! Ö
/;
%8.90# =
:

#//"
'N8-#0 -

'/#"$%&
.$%$.8.
<2-";/#. 7

'/#"$%&
7
.$%$.8. '8.
:

./382-#
0D</%0%"$-"$/% /; 2-#&0 H$%-#@ '0-#41
:

"$.0 K

%8.90#' G
F" 2/&$%
<0/<20
:

T
4-%3$0' "/ P O
?$'"#$98"0
<#0;$D -##-@
%

T '/#"$%&
4/$%
;$20'
O O

'0##1 K

L
S

;$%0#@
2
,#/920. 7 +
,-$%"0# K'
<-#"$"$/% '841 :

"/ '1$< <-4P-&0'


5$"1$% ?
3-@' Y T ;$%0#@
4-<-4$"@ '"-4P
1-#3
K

Ñ;/-$0
T

a0" .$%$.8. 020.0%" J6? T

;#/. '"-4P
7
6A= 0-41

:$.d[+"k$%_D.0:&''$--.
d d
B0209#$"@ <#/920.
$"kl:#'kkklk
:

B$#482-# 1/8#
+=
L-D$.8.
#04"-%&82-#
$%
U C@0 W
-#0-

4/.9/ 8'$%& '"83 K

'-$%"' "@.<-% </80.


K

Ü '0

+
.-D
/; /; 070@5$%3/5'$#:08
.$%
/<
:

A/""0%
/#-%&0' d '841

:9-á._;"5$%'8.Q&P:d;;ON5.
%-#@
'"$;;'
Z
!.-220'" 3$'"$%4" 5$%3/5

'"#0-.
;$#'" %':/;0+-.#0;0-2$%& 41-#-4"0# $%

<3<53" 1-3 .@ @ #0.-$%30#


I/; F %8.90# $% -

5$%3/5
a00P@ 98$23$%&' 1-'1.#;; '1$3$@
B/8%" #070#'-2'
"10
A0./70 F
3$&$"'
18;;.-% 0%4/3$%&
42/%0 '"-4P 5u/ 0D"#-
'<-40
-

4104P .$##/# $%
8'$%&
% "#00
-%@
+

.-"41
3-@
G,6 )R)Q ) :

=-2$3
MD<#0''$/%' '0-#41
H#$%&
&#003@
'.-220'"
20;"
%8.90# "
:

/%

+
d
3$7$30 41/4/2-"0

'<2$" -5-@ 2-#&0'"


'8.
(' )&*&+,
#
- ". / 01+20! 4
$%$
3
#

! "
&' !(

5, !16+
*015+10 '5+1 7&+8
&, !(+
,591
)&*&+, :(,,&;<1 =

>?@$@@A$B?>
#
#

!"
&) < <
C

<
D ,&!E1 +81 !29;10
F(
(! ;1
&+
<50*1
E5! C+ '&+ &!

C C !+

)(
;G H
!11) +(
('
71
F(0+
,(91

,+0&!* 95!&:2<5+&(!
D
JK
C

L
I

&'= +:=MNO
&)15 =

P Q R
I
4

U
S 0 T ,

F
F' >
3

*
> R (

ME* 9&)

$K '&!) 9&) /
44 3

&V 3 $">
=

,&)1 , +
9&)
<1'+
3 =

+05W10,&!* '0(9
/
%K ,+50+

X
3

J A F
71 ,57 G

D ?-J Y
4 5,
+05W10,&!* '0(9 * +( ,
C

.K Z(7 ,+50+ =

&= M[ +( 9&)
1

+85! ?

)&*&+1<191!+
,95<<1,+
,150E8
*011+10
=

+81
&=
*015+10
(!
1 !16+

M8+ 0
\ ,95<<1,+
#
3

]
#
@
#

05!*1 *
=

&+C,
71 ,11
A= Y 5, AA?
"

1<191!+
&' !( ,2E8 &,
'(2!)
01+20! 3 < ^

,95<<1,+
3

D
O

,95<<1,+K
,75'E&*
3

,95<<1,+ K
,75: P
4
=

! 4 & !
3
3

I
3
3
G=_`&=)a 9&)
I

01W10,1
1<191!+, &!

9&)K
05!*1 b[ I

!11) +(
c 5<,( 71
&'
1W1!
!

01W10,1 =

9&) [ 4 V K
05!*1P
! 3 3

1<,1 &' ! &,


()) =

01W&,1

05!*1 P 9&) +% I ! 3
d 3

%=K
(' :1092+5+&(!
16+1!,&(! !16+
M8&, O21,+&(! &, 5!

85W1 !29;10
F2::(,1 G(2
5

9f9ff9 +05W10,1
=&!

'0(9
+81
!
+8(+
3
=

g '&!) 781!
>>% h (EE2001!E1
A@>
h '&0,+
Y i j5<&+ i
?%A 5*(

5E+ i 7&+8
+81 '&0,+
!(7
,75: +( &+ <

'0(9
!
3
4
(EE2001!E1

(8!1
C

5E!1 5 bL` U

0
.
G
%
01W10,1
k % . > @?
4

lmA??Am 3
] $@?A
n19o`
5
M
9&)
=

p ? G !
"
> 9&)
3 %

l 4 % .

_3
4
# # C

+&01 4 =
e
R
=

>?
?AA? Y

&
+ =

? ?$
G G
#

5
#
A$%?.Jm$$%>

=
+;#**<%.JJJA Y

1W1!
&!+

01,2<+ o
A
<(!* <(!*
# E5!
C+
&+
5E&+; 85!)<1
&' 3 44
E500G
<(

E850E018)+E&'&8+OE&V+a=`r
"`
&' P E50!G
3

#
#
01,2<+ o *
+( 85!)<1

s t

E5,1
u5<5

llll t t

t t
+ t t
M++ )) 3

"
5!G ll
llU%. l

78&<1 4(' 3

;011a, =
kadane's algo

binary search
U
Number following a pattern

0f9+B+5)
/

/
:2,8 929
&!E0191!+
C4v``
3>.>
/
99
4
/ 4

$%?@>.rv h,*'
/ :&,8 F+5)1!
/ &!E0191!+

w
(20, o #
19
=

2:)5+1 )&* D
/
::,81a8*

N
I

,+5Ea
'98799 =

We are taking a pattern here and the pattern contains only I and D’s and now our task is to print the output as
numbers.

If the input is IIDDD this means first two numbers are increasing and then next three numbers are decreasing, now
one challenge is that we need to use minimum numbers, so once you see I you can keep on printing 1 and then if
you see D then you have to push it into the stack. The idea of stack is that you will use minimum numbers and once
you reach the end of the input string then you start popping the elements from the stack and start pushing it in the
string end.

:GOE!2>*$x(!,9+!G&9=+
IIDDD )3,:2,8'!29=++K 43,:0,8'27=++i
1
12 (8&+1,+5(a89,<8
Push 3 in stack
push 4 in stack
Push 5 in stack
Reached end?
pop string top and Add 5 to string—>125
Pop string top and add 4 onto the string end—> 1254
pop string top and add 3 onto the string end—> 12543
a5'9,+9G&< =

Now it was the intuition, if the input begins with D push 1 followed by 2 in stack and then keep on adding numbers if
you see other numbers in the input, then again do the same thing that you have to push the numbers

We are simply assigning num=1 and then doing case analysis if character is D then we do something else and if it
is the I then you again do the same thing that you push the number in the stack and then you simply increment
=

number.
Permutation of a given string

C
C

$%. &<

. +&91,
=

C
4

C %.
'1<+ $%.
$$A$%. C

z
y0
4{ z
$%$
3

3 !I

Create a vector of string type ans and then create two strings ip=S and a #null string as op
'0
C
&C
#
Then call function f(ip, op, ans) C C
$%
:
C 3

, $%
This function will do something.
Then we will sort the vector ans.
Now let us see what this function does
We are passing two strings ip and op into the function and passing the vector.
Then we are storing ip.length() in n. If n is 0 then we will simply push op into answer this
means we have passed the ans vector as reference, so we need not worry about anything
simply return, But If n>0 then in that case, we will run a for loop till the length of ip string.
Then we have two strings op1 and ip1 and we are copying op and ip into those and then
we are pushing ip[I] in op[I] and then we are erasing ip1 from start to ith index and then we
are calling f(ip1, op1,ans)
Longest palindrome in a string

/
*5:
| D
*5: +( ! C

&f{fv
C

&+, b| i
|

3
<
g 4 0 , +I +8&, 4, A

of+vfv'MvNff
3
=
4 E 8 5 0 5 8 5 ,

k.N''''M_ 956)'
l?
3

o
f
C

NM'''
Y
H

,
8 M ' '
v?
b
M '
Substrings of length k with k-1 elements
Repeated string match

Rolling hash method

You might also like