Skip to content

Instantly share code, notes, and snippets.

View Ifihan's full-sized avatar
🔧
Work in Progress

Ifihanagbara Olusheye Ifihan

🔧
Work in Progress
View GitHub Profile
@Ifihan
Ifihan / main.md
Created January 3, 2025 21:35
Number of Ways to Split Array

Question

Approach

Approach 1

My first approach was to create count and left_arr varaible to store my valid count and left split.

I then iterated through the array, stopping at the second-to-last index. This is because a valid split must leave at least one element on the right side. At each iteration, I appended the current element (nums[i]) to left_arr, gradually building the left portion of the split. To define the right portion, I took a slice of the array starting from the next index (i + 1) to the end, ensuring the right side always included all elements after the split point.

@Ifihan
Ifihan / main.md
Last active January 4, 2025 22:32
Unique Length-3 Palindromic Subsequences

Question

Approaches

Approach 1

My first approach was to map out all the unique palindromes of length three using three nested loops. I then maintained a set to store the results, ensuring each palindrome was counted only once.

To check for all valid palindromes, I iterated through the string s using three indices (i, j, k), where (i < j < k). For each triplet, I checked if the characters at indices (i) and (k) matched, as this is the necessary condition for a length-3 palindrome. If they matched, I formed the subsequence using (s[i]), (s[j]), and (s[k]) and added it to the set of results.

@Ifihan
Ifihan / main.md
Created January 5, 2025 21:29
Shifting Letters II

Question

Approach

I thougt of this first...

My first approach to the problem was to directly apply each shift operation to the string. Then I remembered the shege I've seen in the hand of TLE but here is my first thought. First, I convert the input string s into a list and also store the entire English alphabet as a string ("abcdefghijklmnopqrstuvwxyz") so I can easily reference the positions of each character during the shifts.

Next, I iterate through the shifts array, where each shift operation consists of three values: start, end, and direction. The start and end values define the range of indices in the string that I need to modify, and direction tells me whether to shift the characters forward (1) or backward (0).

@Ifihan
Ifihan / main.md
Created January 6, 2025 22:44
Minimum Number of Operations to Move All Balls to Each Box

Question

Approach

I thought of a brute force approach first...

Using this approach, I iterated through each box (i) and computed the total number of operations required to move all the balls to that box. For each box (i), I iterated over all other boxes (j), and then calculated the distance from (j) to ( i), and add the distance to the total if the (j)-th box contains a ball (boxes[j] == '1'). It works even though it's an O(n^2) solution, no TLE today thank God. But this approach won't work for larger constraints, so let's use prefix sum!

Moving to prefix sums

@Ifihan
Ifihan / main.md
Last active January 7, 2025 08:22
String Matching in an Array

Question

Approach

My approach begins by creating a dictionary called word_lengths to precompute the lengths of all the words in the input words list. This dictionary helps to efficiently track the length of each word and minimizes the need for recalculating word lengths during comparisons.

Next, I define a res array to store the words that meet the substring condition. Then, I iterate through the list words using a nested loop. For the outer loop, I keep track of the current word as word and its index i. For the inner loop, I go through each word again, referring to it as other_word and keeping its index as j.

Inside the nested loop, I add a check to skip comparing a word with itself by ensuring i != j. If this condition passes, I then proceed to check two things:

  1. If the length of word is less than or equal to the length of other_word. This ensures that word can p
@Ifihan
Ifihan / main.md
Created January 8, 2025 22:38
Count Prefix and Suffix Pairs I

Question

Approach

Instead of using functions like startswith or endswith, I used an approach to verify both the prefix and suffix conditions manually.

The first step was to define the isPrefixAndSuffix function. This function checks whether one string is both a prefix and a suffix of another. To do this, the function compares the characters of str1 with the corresponding characters in str2. For the prefix condition, the function iterates over the characters of str1 and verifies that they match the corresponding characters in str2 starting from the beginning. Similarly, for the suffix condition, it iterates over the characters of str1 and ensures they match the corresponding characters in str2 starting from the end. If both conditions are satisfied, the function returns True; otherwise, it returns False.

The second step was to count all valid pairs of indices `(i, j)

@Ifihan
Ifihan / main.md
Created January 9, 2025 22:29
Counting Words With a Given Prefix

Question

Approach

To solve the problem, I iterated directly over the list.Then I checked if the prefix pref matched the initial substring of the word. This was achieved by slicing the string word up to the length of pref and comparing it with pref. If they matched, I incremented a counter, which I returned at the end of the function.

Implemtation

class Solution:
 def prefixCount(self, words: List[str], pref: str) -&gt; int:
@Ifihan
Ifihan / main.md
Created January 10, 2025 10:20
Word Subsets

Question

Approach

The first step was to precompute the maximum character frequencies across all the strings in words2. For example, if words2 contained the strings ["l", "e"], I determined the highest number of times each character appeared in any of these strings.

Next, for each string in the words1 list, I computed its character frequencies and compared them against the precomputed maximum frequencies from words2. If a string in words1 met or exceeded the frequency requirement for every character in the unified representation, it was deemed universal. For example, a string like "google" would pass because it contained at least one 'e' and one 'o', meeting the requirements set by words2 = ["e", "o"]. A string like "amazon" would fail because it didn’t have an 'e'.

Implmentation

@Ifihan
Ifihan / main.md
Created January 11, 2025 22:33
Construct K Palindrome Strings

Question

Approach

I used Python's Counter to create a hashmap that stored the frequency of each character in the string. Then I iterated through the hashmap and counted all the characters with odd occurrences. This value, was called odd_count, and it was to measure whether constructing k palindromes was feasible.

Next, I implemented the conditions to if k was greater than the length of the string because, it would be impossible to construct k palindromes. Also, I compared odd_count with k, knowing that k must be at least as large as odd_count since each palindrome can only handle one odd-frequency character.

Then I return True if the conditions were satisfied, showing thatk palindromes was possible.

@Ifihan
Ifihan / main.md
Created January 12, 2025 22:28
Check if a Parentheses String Can Be Valid

Question

Approach

First, I checked if the length of the string s is odd. If it is, then I return False in that case.

Next, I realized that balancing the parentheses is a good way to solve the problem. In the first pass, I traversed the string from left to right. I used two counters: one to track the possible number of opening parentheses open_possible, including the flexibility from locked[i] == '0', and another to track the number of fixed closing parentheses close_possible. As I went through the string, I incremented open_possible for either an opening parenthesis or an unlocked position (locked[i] == '0) that could be converted into one. Also, I incremented close_possible for each fixed closing parenthesis. At any point, if close_possible exceeded open_possible, it meant there were too many closing parentheses to ever balance the string, and I returned False.

Afte