This article mainly introduces the method of PHP to obtain the first non-repeating character in the character stream, involving PHP's traversal of the index array and judgment related operation skills. Friends who are interested in PHP can refer to it Next article
The example of this article describes how PHP obtains the first non-repeating character in the character stream. Share it with everyone for your reference. The details are as follows:
Question
Please implement a function to find the first character in the character stream. A character that appears once. For example, when reading only the first two characters "go" from the character stream, the first character that appears only once is "g". When the first six characters "google" are read from this character stream, the first character that appears only once is "l".
Output description:
If there is no character that appears once in the current character stream, return # characters
Solution
Use index Array
Implementation code
<?php global $result; //Init module if you need function Init(){ global $result; $result = []; } //Insert one char from stringstream function Insert($ch) { global $result; // write code here if(isset($result[$ch])){ $result[$ch]++; }else{ $result[$ch] =1; } } //return the first appearence once char in current stringstream function FirstAppearingOnce() { global $result; foreach($result as $k =>$v){ if($v ==1){ return $k; } } return "#"; }
The above is all the content of this article, I hope it will be helpful to everyone's learning!
Related recommendations:
php stringfunction
php string filtering and conversion function
php string built-in function summary
The above is the detailed content of PHP method to get the first non-repeating character in a character stream. For more information, please follow other related articles on the PHP Chinese website!