Advance Practical PHP Big O Notation Non-repeating - video 115W
Автор: OldManPHP
Загружено: 2026-02-19
Просмотров: 9
Описание:
I want to mentioned the crc32() function.
The crc32() function calculates a 32-bit CRC (cyclic redundancy checksum) for a string. This function can be used to validate data integrity.
Example:
$str = crc32("Hello World!”);
printf("%u",$str);
The Challenge: First Non-Repeating Character
Goal: Given a string (e.g., "aabbcdeeff"), find the first character that only appears once.
Using a Hash Map makes this efficient (O(n)) because we only need to pass through the string twice: once to count, and once to find the first “1".
Hash map approach is the "gold standard" for the non-repeat problem.
The PHP Solution
/**
Finds the first character in a string that does not repeat.
* @param string $string The input string to search.
@return string|null The first unique character, or null if none exist.
*/
function firstUniqueChar($string) {
// Initialize an associative array to store the frequency of each character.
// In PHP, associative arrays function as Hash Maps.
$charCounts = [];
$length = strlen($string);
// -- Pass 1: Frequency Counting --
// We loop through the string to populate our map.
for ($i = 0; $i $length; $i++) {
$char = $string[$i];
// If the character is already a key, increment its count.
// Otherwise, initialize it at 1.
if (i s s e t ($charCounts[$char])) {
$charCounts[$char]++;
} else {
$charCounts[$char] = 1;
}
}
// -- Pass 2: Identification --
// We loop through the string again in its original order.
for ($i = 0; $i $length; $i++) {
// We look up the current character's count in our Hash Map.
// The first one we hit with a value of exactly 1 is our winner.
if ($charCounts[$string[$i]] === 1) {
return $string[$i];
}
}
// If the loop finishes without returning, no unique characters exist.
return null;
}
// Example usage:
echo firstUniqueChar("stress"); // Output: t
Why is this better than the alternative?
Without a Hash Map, you would have to use a nested loop (checking every character against every other character).
Nested Loop: O(n^2) — If the string is 10,000 characters, it takes 100,000,000 operations.
Hash Map: O(n) — If the string is 10,000 characters, it takes roughly 20,000 operations.
Summary of What We've Covered
Direct Mapping: Using keys for instant access.
Frequency Counting: Using values to track occurrences.
Collisions: How PHP handles "overlaps" using chaining.
PHP Array Functions: Using array_filter, array_reduce, and array_map for clean code.
One last thing for your toolkit:
If you ever need to turn a Hash Map back into a simple indexed list of its keys, use array_keys($myMap).
This is super helpful when you've used a map to filter data and just want the final list of "IDs" or "Names."
—————————————————-
Here are the html/scripts in an txt and php extension.
——————————————————-
——————————————————-
https://convertowordpress.com/dataStr...
——————————————————-
——————————————————-
Check out the PHP manual that is available online:
https://www.php.net/docs.php
If you want a developer to create your web design project.
Visit: https://convertowordpress.com
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: