0% found this document useful (0 votes)
11 views

String Vikram

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

String Vikram

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

String

sequence of characters.
1) it consider as data type & array of bytes that store sequence of char => str
2) str => arry of char => str terminated by '\0' = >string ka end batane ke liye ek special character hota hai, jise null character

How String is represented in Memory?


1) in c => represent by char ptr or char array
char arr
2) if str declare as => local var inside fun => it stored in => stack of memory
3) str declare => global or static var => store in => data segment
char ptr
1) it points to memory loc where string is stored => use in dynamic allocation

'ptr' me 'c' ka address store hota hai

op on string Get a Sub-String after a Character


1) concatenation of str => combine more than one str together (s1 + s2 + s3) s = "dog:cat" ;
(a) s1.append( s2 ) (b) s1 + s2 (c) strcat ( s1 , s2 ) pos = s.find ( ":")
sub = s.substr(pos+1)
cout<< sub ; cat
find => given char / complete string
find char in str Get a SubString Before a Character?
s.find ( sub) // find 1st occurrence of sub-str in given string sub = s.substr ( 0, pos ) ;
s.find( sub, pos ) // substr cout << sub ; // dog
s.find( sub, pos , n) // n char of sub
Print all Sub-Strings of a Given String?
if( s.find ( sub ) ! = string :: npos ) { cout << res; } "abcd" => a,ab,abc,abcd,b,bc,bcd,c,cd,d
for ( i=0; i<n; i++ ) { t.c => o(N^3)
find sub str in another str for ( len=1; len<=n-i; len++) {
s1 ( n ) & sub-str ( M ) cout << s.sub( i, len ) <<endl; }
outer loop -> 0 to N-M
inner loop -> 0 to M Print Sum of all Substrings of a String Representing a no
every idx check if sub-str traversed by inner loop is given sub str or not "1234" => 1670

sub str in c++

t.c = o(n^3) sc = O(n)

Print the Maximum Value of all Substrings of a String Representing a Number


s="823" => All substrings are { 8, 82, 823, 2, 23, 3 } => 823 max
tc = O(n^3) sc = o(n!)

823

8
traverse the string S and when any string S1 is found as a substring in Finding the Length of String
Replace in String the string S then replace it by S2

using inbuilt methods:

s.size() s.length() both are same => return leng of str

Reverse and Rotation of a String

Rotations of a String:
Generate all rotations of a given string

Reverse a String:
Trim a String =>
Input: str = “Hello World”
Remove spaces from a given string
Output: dlroW olleH
O(n) O(1)
reverse ( s.begin () , s.end () ) ;
s = string(s.rbegin(), s.rend()); O(n) O(n)

Stack

O(n) O(n)

Two Pointer Technique

O(n) O(1)
7. Subsequence of a String => sub-seq => selecting some or all characters in Print all substring of a number without any conversion
the same order, without rearranging them. input: N = 12345
sub-string is a contiguous part of a string Output: Possible Substrings: {1, 12, 123, 1234, 12345, 2, 23, 234, 2345, 3, 34, 345, 4,
n size str => n*( n + 1 ) / 2 => non empt sub-str 45, 5}
n size str => ( 2^n )-1 sub-seq

Number of substrings of one string present in other

Time Complexity: O(n*n*n)


Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Auxiliary Space: O(1)
10. Palindrome String => reverse of the string is the same as the string. Using Two Pointers – O(n) Time and O(1) Space

comparing original and reversed strings – O(n) Time and O(n) Space

Check if actual binary representation of a number is palindrome Reverse actual bits of the given number
Check if two strings are same or not
Length of a String Input: s1 = “abc”, s2 = “abc”
Output: Yes

Insert a character in String at a Given Position

TC: O(n),

Built-In Methods
String Matching Algorithms => find pattern in given str

Time Complexity: O(N2)


Auxiliary Space: O(1)

KMP Pattern Searching Algorithm


Top 50 interview coding question

approach
Longest Common Prefix using Sorting
sort arr of str -->

Time Complexity: O(n*m*log n),

Auxiliary Space: O(m) to store the strings first, last and result.

Roman Number to Integer


Reverse Words in a String
Remove Outermost Parentheses
Input: s = "(()())(())"
Output: "()()()"

4. Valid Parentheses
22. Generate Parentheses
a-2

856. Score of Parentheses


Given a balanced parentheses string s, return the score of the string.

The score of a balanced parentheses string is based on the following rule:

- "()" has score 1.


- AB has score A + B, where A and B are balanced parentheses strings.
- (A) has score 2 * A, where A is a balanced parentheses string.

Time Complexity : O(N)

Space Complexity : O(N)


String Compression

question special bec compressed string s should not be returned separately, but instead, be stored in the input character array chars

T.C : O (n)
S.C : O(1)

take arra of size 26


Check if the Sentence Is Pangram
1 traversal

tc = O( n )
2 traversal
Most ask string q in interview
1) Reverse Words

Using Stack
Time complexity: O(n)
Auxiliary Space: O(n)

Using Two Reverses:


->Pehle hum poori string ko reverse
karte hain.
->Uske baad, reverse ki gayi string ke Time Complexity: O(n)
har word ko individually reverse karte Auxiliary Space: O(1)
hain.
Longest Common Prefix using Sorting

Time Complexity: O(n*m*log n), to sort the array, where n is the number of strings and m is the length of longest
string.
Auxiliary Space: O(m) to store the strings first, last and result.
Longest Common Prefix using
Word by Word Matching

Time Complexity : O(n*m), as we are iterating through all the strings and for each string we are iterating though
each characters, where n is number of strings and m is length of the longest string.
Auxiliary Space : O(m), to store the longest prefix string.

Longest Common Prefix using Character by Character Matching


Character-by-character matching works faster than
word-by-word matching.

Time Complexity: O(n*m), where n is number of strings


and m is length of the shortest string.
Auxiliary Space: O(m), to store the longest prefix string.
Longest Common Prefix using Binary Search

Time Complexity : O(n*m),

Auxiliary Space: O(m), to store the longest


prefix string.
Find the minimum distance between the given two words

Time Complexity: O(N*L), where N is number of strings


and L is size of maximum string.
Auxiliary Space: O(1)

Encrypt the string – 2

Time Complexity: O(N)


Auxiliary Space: O(N)
Find an equal point in a string of brackets

Auxiliary Space: O(N)

Time Complexity: O(N), Where N is the


size of given string
Auxiliary Space: O(1)
Isomorphic Strings Check
Check if two strings are k-anagrams or not

You might also like