Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find first non-repetitive character from a string?

Tags:

string

php

I've spent half day trying to figure out this and finally I got working solution. However, I feel like this can be done in simpler way. I think this code is not really readable.

Problem: Find first non-repetitive character from a string.

$string = "abbcabz"

In this case, the function should output "c".

The reason I use concatenation instead of $input[index_to_remove] = '' in order to remove character from a given string is because if I do that, it actually just leave empty cell so that my return value $input[0] does not not return the character I want to return.

For instance,

$str = "abc";
$str[0] = '';
echo $str;

This will output "bc"

But actually if I test,

var_dump($str);

it will give me:

string(3) "bc"

Here is my intention:

Given: input

while first char exists in substring of input {
  get index_to_remove
  input = chars left of index_to_remove . chars right of index_to_remove

  if dupe of first char is not found from substring
     remove first char from input 
}
return first char of input

Code:

function find_first_non_repetitive2($input) {

    while(strpos(substr($input, 1), $input[0]) !== false) {

        $index_to_remove = strpos(substr($input,1), $input[0]) + 1;
        $input = substr($input, 0, $index_to_remove) . substr($input, $index_to_remove + 1);

        if(strpos(substr($input, 1), $input[0]) == false) {
            $input = substr($input, 1);     
        }
    }
    return $input[0];
}
like image 373
Meow Avatar asked Jun 02 '10 06:06

Meow


People also ask

How do I find the first non repeated character of a string in Python?

"": slen0 = len(myStr) ch = myStr[0] myStr = myStr. replace(ch, "") slen1 = len(myStr) if slen1 == slen0-1: print ("First non-repeating character = ",ch) break; else: print ("No Unique Character Found! ")


3 Answers

<?php
    // In an array mapped character to frequency, 
    // find the first character with frequency 1.
    echo array_search(1, array_count_values(str_split('abbcabz')));
like image 135
Casey Chu Avatar answered Nov 04 '22 08:11

Casey Chu


Python:

def first_non_repeating(s):
 for i, c in enumerate(s):
  if s.find(c, i+1) < 0:
   return c
 return None

Same in PHP:

function find_first_non_repetitive($s)
{
 for($i = 0; i < strlen($s); i++) {
  if (strpos($s, $s[i], i+1) === FALSE)
   return $s[i];
 }
}
like image 40
Zart Avatar answered Nov 04 '22 09:11

Zart


Pseudocode:

Array N;

For each letter in string
  if letter not exists in array N
    Add letter to array and set its count to 1
  else
    go to its position in array and increment its count
End for

for each position in array N
  if value at potition == 1
    return the letter at position and exit for loop
  else
    //do nothing (for clarity)
end for

Basically, you find all distinct letters in the string, and for each letter, you associate it with a count of how many of that letter exist in the string. then you return the first one that has a count of 1

The complexity of this method is O(n^2) in the worst case if using arrays. You can use an associative array to increase it's performance.

like image 28
Sev Avatar answered Nov 04 '22 09:11

Sev