About Course
This lecture delves into fundamental problems related to arrays and strings, providing multiple approaches to solve each problem in C++.
Problem 1: Finding the First Unique Character in a String
The lecture begins with a solution to find the first unique character in a string using a brute force approach. The `Search` function checks if a character is repeated by scanning the substring from a given start index to the end of the string. The `IsUnique` function iterates through each character, using `Search` to determine if the character is unique. The time complexity of this approach is O(n2) due to the nested loop structure.
For efficiency, two alternative methods are presented:
1. Using a Hash Map: This method counts character occurrences in a first pass and identifies the first unique character in a second pass. It has a time complexity of O(n) and a space complexity of O(1) due to the limited character set.
2. Using an Array: Assuming only lowercase letters, this method uses a fixed-size array to count character occurrences. Like the hash map approach, it achieves O(n) time complexity and O(1) space complexity.
Problem 2: Check Permutation
Two methods are provided to check if two strings are permutations of each other:
1. Sorting Method: Both strings are sorted and then compared. This method has a time complexity of O(n log n).
2. Using Extra Space: A character count array tracks the frequency of each character. The counts are adjusted based on the second string, and the array is checked to ensure all counts return to zero, indicating the strings are permutations. This method operates in O(n) time complexity.
Problem 3: URLify
The task is to replace all spaces in a string with ‘%20’. Two methods are provided:
1. Inefficient Space Replacement: This method iteratively shifts characters to the right to insert ‘%20’, leading to O(n2) time complexity.
2. Efficient Replacement: This approach counts spaces first and then modifies the string in reverse order, achieving O(n) time complexity.
Problem 7: String Compression
A method is implemented to compress strings by counting consecutive characters. The function checks if the compressed string is shorter than the original and returns the appropriate version. The solution operates in O(n) time complexity.
Problem 8: IsZero Matrix
This problem involves setting the entire row and column to zero if an element in an MxN matrix is zero. The function `IsZeroMatrix` checks if a matrix contains only zeros. A related problem is rotating a matrix by 90 degrees. The provided solution transposes the matrix and then reverses each row, achieving O(n2) time complexity.
Problem 9: String Rotation
To check if one string is a rotation of another, the `isRotation` function concatenates the first string with itself and checks if the second string is a substring of this new string. This approach leverages the `isSubstring` function and operates in O(n) time complexity.
Conclusion
This lecture effectively covers essential algorithms for handling arrays and strings, emphasizing both brute force and optimized solutions. By comparing different methods for each problem, the lecture illustrates the importance of algorithm efficiency in practical applications. These solutions provide a robust foundation for tackling similar problems in more complex scenarios.
Course Content
Arrays and Strings
-
Unique Characters in String Way 1 Brute Force O(N^2)
14:18 -
Unique Character in a string using Extra Space
10:22 -
IsUnique Fast
06:27 -
Check Permutation By Sorting Method
06:29 -
Permutation Batter Implement
08:15 -
URLIFY
21:13 -
String Compressor
12:38 -
IsZeroMatrix & Rotate Matrix
10:13 -
Rotatition of String
17:08