0% found this document useful (0 votes)
7 views16 pages

Strings 2 Annoted

The document discusses strings in C++. It covers sorting strings, anagrams, counting unique characters, splitting a sentence into individual words, and other string manipulation problems. Example problems and solutions are provided for longest common prefix, isomorphic strings, finding the string with maximum numeric value, and more.
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)
7 views16 pages

Strings 2 Annoted

The document discusses strings in C++. It covers sorting strings, anagrams, counting unique characters, splitting a sentence into individual words, and other string manipulation problems. Example problems and solutions are provided for longest common prefix, isomorphic strings, finding the string with maximum numeric value, and more.
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/ 16

C++

Strings 2
Lecture- 18

Raghav Garg
Warmup Problem : Input a string and return the
number of times the neighbouring characters are
different from each other.

string
"abb
str: deffghh";
A&BYS
count=
Sorting a string
A

Sort -> asciivalues he

using in-built orderme sort Karke

function
de
dega

strings:"name";
Sort (S. begin(), s.end());

S -aemn
Ques : Given two strings s and t, return true if t is an
anagram of s, and false otherwise. leet code 242
+

Sorting - builtin

Input : s = physicswallah, t = wallahphysics


Output: YES

aba ca b
-
anagrams
a

Ques : Given Ne
n stringsYe consisting of lowercase
English alphabets. Print the character that is
occurring most number of times. Extra space 26 size ->

max count 0; constant =

strings:"leetcode";
space
special away - int air (267

b d e f g h i j k
1mnopqrsturwx 2
a c
y

10/01010101011010111101010/11010101010)
6 8 9 11 12 13 14 15
0 1 2 3 4 5 7 10
16171819204222324 2S
Stringstream
class
strings:"Raghar
is a mathe teacher";
d
to the words
⑨ is calculate no.
of
Ques : Given a sentence, split every single
word of the sentence and print in a new line.
done
Already

strings;
getline (cin,s);

string temp;
stringstream ss(S);
While (stemp)
cout
tempendl;
Ques : Given a sentence ‘str’, return the word
that is occurring most number of times in that
sentence.
string str:
"Raghar is a maths teacher. He is a DSA
"

mentor as well max Count =%$2

vector strings v; count 04


=

AIIXAR
y VIX I

v ("DSA", "He","a", "a","as", "is"


=
"is", "math", "mentor",

"raghar", "teacher", "well?" I


stoi vs stoll built-in functions
↓ -
string to
long long
string to integer ↓
63
L -
263 to 2 -
I
31
-
2Y Ho I -
1
- vector
Ques : Given n stringS consisting of digits from
0 to 9. Return the index of string which has
maximum value. (Take 0 based indexing)

Input : 0123, 0023, 456, 00182, 940, 2901


Output: 5

~=["O123", "0023", "456","00182", "940"," 2901"7


A
Ques : Input n strings and write a program to find
the longest common prefix string of all the strings.

["dog", "racecar", "car I


17

stre=

->

["car", "dog", "racecar"7


Ques : Input n strings and write a program to find
the longest common prefix string of all the strings.
[Leet Code -14]
strs=2 "flower", "flow", "flight")
Ques : Given two strings s and t, determine if
X

they are isomorphic. [Leetlode 285]

"add" true
S: "egg" t =

"bar" false
S "foo"
= t =

true
t :"title"
s =
"paper"
#Hint: special away
Ques : Given two strings s and t, determine if
they are isomorphic.
S "title"
= t "paper"
=

(t)
#
Ques : Given two strings s and t, determine if
X

they are isomorphic. [LeetCode 20S]


-

S = "bade" t "baba"
=

alado
e
Thank you!

You might also like