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

DSA AND ALGO

The document contains a list of programming problems categorized by difficulty (Easy, Medium, Hard) along with their corresponding problem numbers. It also includes tips for solving string-related problems, common approaches, and examples of string functions in programming. Additionally, it outlines strategies for tackling various problem types, such as using hashmaps, sliding windows, and two pointers.

Uploaded by

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

DSA AND ALGO

The document contains a list of programming problems categorized by difficulty (Easy, Medium, Hard) along with their corresponding problem numbers. It also includes tips for solving string-related problems, common approaches, and examples of string functions in programming. Additionally, it outlines strategies for tackling various problem types, such as using hashmaps, sliding windows, and two pointers.

Uploaded by

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

ARRAY

SI.NO EASY DATE


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
STRING
SI.NO EASY DATE MEDIUM
1941. Check if All Characters Have Equal Number of Occurrences
1 3. Longest Substring Without Repeating Characters
2 13. Roman to Integer 151. Reverse Words in a String

14. Longest Common Prefix


3 2390. Removing Stars From a String

20. Valid Parentheses


4 443. String Compression

28. Find the Index of the First Occurrence in a String


5 1456. Maximum Number of Vowels in a Substring of Given Length

6 125. Valid Palindrome 1657. Determine if Two Strings Are Close

7 2185. Counting Words With a Given Prefix

8 345. Reverse Vowels of a String

9 392. Is Subsequence

10 796. Rotate String

11 1071. Greatest Common Divisor of Strings


12 3042. Count Prefix and Suffix Pairs I
13 1408. String Matching in an Array

14 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence

15 3407. Substring Matching Pattern


16 1668. Maximum Repeating Substring
17 1768. Merge Strings Alternately
18 1790. Check if One String Swap Can Make Strings Equal

19

20

21
22
23
24

25

26

27

28

29
30
31
32
33

34
35
36
37
38
39
40
DATE HARD DATE
TIPS
1. Identify the Problem Goal 2. Check Constraints

Is it about checking characters? → Use hashmaps or


frequency arrays String length ≤ 100? → Brute force (O(n²)) might work.
Is it about finding substrings? → Use sliding window or String length is large (~10⁵ or more)? → Need efficient
two pointers O(n) approach, like:
Does it involve balancing or removing characters? → Use Sliding Window
stacks
Is it about merging or rearranging strings? → Use sorting Two Pointers
or greedy
Trie (for prefix searches)
KMP Algorithm (for pattern matching)

4. Recognizing Common Patterns

Problem Type Common Approach


Character Frequency HashMap, Array
Finding Substring Sliding Window
Checking Validity Stack
Reversing Characters Two Pointers
Pattern Matching KMP Algorithm, Trie
Merging or Sorting Strings Sorting, Greedy

5. Solve Using a General Strategy Example: Applying This Method

Problem: "3. Longest Substring Without Repeating


Step 1: Try brute force (O(n²)). Characters"
Step 2: Look for repeated calculations → Can we use
memoization or hashmaps?
Step 3: If it's about a window or range, try Sliding Window ✅ Goal: Find the longest contiguous substring without
(O(n)). duplicate characters.
Step 4: If it's about balancing/removal, try Stack. ✅ Constraints: Large input (~10⁵), so O(n²) brute force is too
slow.
Step 5: If sorting helps, use Sorting + Greedy approach ✅ Pattern: We need to track the last seen index of characters
→ Sliding Window + HashMap.

Final Approach:

Use a HashMap to track last seen index.


Use Sliding Window to expand when unique, shrink when
duplicates appear.
Time Complexity: O(n), Space Complexity: O(1) (for 26
lowercase letters only)
🔹 C++ String Functions with Examples

3. Look at Input & Output Function

s.length() / s.size()

If a boolean answer is expected: s.empty()

Likely hashmap, set, or two-pointer approach s[0] / s.at(i)

Example: "Is a string a palindrome?" (Two Pointers) s.front()

If we need a transformed string: s.back()

Likely stack or two-pointer swapping s.append("text")

Example: "Reverse words in a string" s += "text"

If we need to count something: s.insert(pos, "text")

Likely frequency count (hashmap or array) s.erase(pos, len)

s.replace(pos, len,
Example: "Count occurrences of substrings" "text")
s.find("text")
s.rfind("text")

s.substr(pos, len)

Example Problems s.compare("text")

1941, 1657, 2185 s == s2 / s != s2


3, 1456 s < s2 / s > s2
20, 2390 s.swap(s2)
reverse(s.begin(),
125, 345 s.end())
sort(s.begin(),
28, 392, 1455 s.end())
toupper(ch) /
14, 1768, 1668 tolower(ch)
isdigit(ch)
isalpha(ch)
isspace(ch)

stoi(s) / stol(s) /
stoll(s)

to_string(num)
ng Functions with Examples

Description Example

Returns the length of the string. "hello".length() → 5

Checks if the string is empty (true or false). string s = ""; s.empty(); // true

Accesses the character at index i. string s = "apple"; s[1]; // 'p'

Returns the first character. "hello".front() → 'h'

Returns the last character. "hello".back() → 'o'

Adds text to the end of the string. string s = "hi"; s.append(" there"); // "hi there"

Another way to append text. string s = "hi"; s += " there"; // "hi there"

Inserts text at a specific position. string s = "hell"; s.insert(4, "o"); // "hello"

Removes a part of the string. string s = "abcdef"; s.erase(2, 2); // "abef"

Replaces a part of the string with new text. string s = "abcdef"; s.replace(2, 3, "XYZ"); //
"abXYZf"
Finds the first occurrence of a substring. "hello world".find("lo") → 3
Finds the last occurrence of a substring. "banana".rfind("na") → 4

Extracts a substring. "hello".substr(1, 3) → "ell"

Compares two strings lexicographically. "apple".compare("banana") → negative value

Checks if two strings are equal or not. "abc" == "abc" → true


Lexicographical comparison. "apple" < "banana" → true
s1="abc", s2="xyz"; s1.swap(s2); // s1="xyz",
Swaps the content of s and s2.
s2="abc"
string s = "hello"; reverse(s.begin(), s.end()); //
Reverses the string.
"olleh"
string s = "dcba"; sort(s.begin(), s.end()); //
Sorts the string lexicographically.
"abcd"
Converts a character to
uppercase/lowercase. char ch = 'a'; toupper(ch); // 'A'

Checks if a character is a digit. isdigit('5') → true


Checks if a character is a letter. isalpha('a') → true
Checks if a character is a whitespace. isspace(' ') → true

Converts string to integer (int, long, long


stoi("123") → 123
long).

Converts a number to string. to_string(42) → "42"


ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE
ARRAY
SI.NO EASY DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ARRAY
MEDIUM DATE
HARD DATE

You might also like