Seed Programming

School of Seed Programming Logo

1. Arrays and Strings

Wishlist Share
Share Course
Page Link
Share On Social Media

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.


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.


Show More

What Will You Learn?

  • Brute force approach for finding unique characters.
  • Efficient character counting with hash maps and arrays.
  • Methods to check if two strings are permutations.
  • Space replacement techniques in strings.
  • String compression by counting consecutive characters.
  • Matrix manipulation for zero checks and rotations.
  • Efficient string rotation verification.
  • Importance of algorithm efficiency and comparing different methods.

Course Content

Arrays and Strings

  • Unique Characters in String Way 1 Brute Force O(N^2)
  • Unique Character in a string using Extra Space
  • IsUnique Fast
  • Check Permutation By Sorting Method
  • Permutation Batter Implement
  • String Compressor
  • IsZeroMatrix & Rotate Matrix
  • Rotatition of String

Student Ratings & Reviews

No Review Yet
No Review Yet
Open chat
Hello 👋
Can we help you?
Need more information about 1. Arrays and Strings