views:

286

answers:

8

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];
}
+1  A: 

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.

Sev
+1  A: 

This can be done in much more readable code using some standard PHP functions:

// Count number of occurrences for every character
$counts = count_chars($string);

// Keep only unique ones (yes, we use this ugly pre-PHP-5.3 syntax here, but I can live with that)
$counts = array_filter($counts, create_function('$n', 'return $n == 1;'));

// Convert to a list, then to a string containing every unique character
$chars = array_map('chr', array_keys($counts));
$chars = implode($chars);

// Get a string starting from the any of the characters found
// This "strpbrk" is probably the most cryptic part of this code
$substring = strlen($chars) ? strpbrk($string, $chars) : '';

// Get the first character from the new string
$char = strlen($substring) ? $substring[0] : '';

// PROFIT!
echo $char;
Alexander Konstantinov
+1  A: 

1- use a sorting algotithm like mergesort (or quicksort has better performance with small inputs)
2- then control repetetive characters

  • non repetetive characters will be single
  • repetetvives will fallow each other

Performance : sort + compare
Performance : O(n log n) + O(n) = O(n log n)
For example

    $string = "abbcabz"

    $string = mergesort ($string)
    // $string = "aabbbcz" 

Then take first char form string then compare with next one if match repetetive
move to the next different character and compare
first non-matching character is non-repetetive

estergones
First non-matching character in the sorted string ("aabbbcz") won't necessary will be the same as the first non-matching character in the original string ("abbzabc" for example).
Alexander Konstantinov
+1  A: 

Here's a function in Scala that would do it:

def firstUnique(chars:List[Char]):Option[Char] = chars match { 
  case Nil => None
  case head::tail => {
    val filtered = tail filter (_!=head)
    if (tail.length == filtered.length) Some(head) else firstUnique(filtered)
  }
}

scala> firstUnique("abbcabz".toList)
res5: Option[Char] = Some(c)

And here's the equivalent in Haskell:

firstUnique :: [Char] -> Maybe Char
firstUnique [] = Nothing
firstUnique (head:tail) = let filtered = (filter (/= head) tail) in
            if (tail == filtered) then (Just head) else (firstUnique filtered)

*Main> firstUnique "abbcabz"

Just 'c'

You can solve this more generally by abstracting over lists of things that can be compared for equality:

firstUnique :: Eq a => [a] -> Maybe a

Strings are just one such list.

pelotom
+1  A: 
$str="abbcade";
$checked= array(); // we will store all checked characters in this array, so we do not have to check them again

for($i=0; $i<strlen($str); $i++)
{
    $c=0;
    if(in_array($str[$i],$checked)) continue;

    $checked[]=$str[$i];

    for($j=$i+1;$j<=strlen($str);$j++)
    {
        if($str[$i]==$str[$j]) 
        {
            $c=1;
            break;  
        }
    }
    if($c!=1) 
    {
        echo "First non repetive char is:".$str[$i]; 
        break;
    }
}
nik
+5  A: 
<?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')));
Casey Hope
+1 for shortness
Willi
+2 for shortness, thanks!
masato-san
+1  A: 

This should replace your code...


$array = str_split($string);
$array = array_count_values($array);
$array = array_filter($array, create_function('$key,$val', 'return($val == 1);'));
$first_non_repeated_letter = key(array_shift($array));

Edit: spoke too soon. Took out 'array_unique', thought it actually dropped duplicate values. But character order should be preserved to be able to find the first character.

bob-the-destroyer
+1  A: 

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];
 }
}
Zart
Actually this won't work for string such as "bbc".<br />It will return 2nd "b"<br /><$i = 0 > "bbc"<br />if strpos("bbc", "b", 0+1) is not false so skip the return.<br /><$i = 1> "bbc"<br />if strpos("bbc", "b", 1+1) is false thus returns $s[1] which is "b"<br />In this case we need to return "c" instead.<br />
masato-san
Ouch... This should be solved by adding array/set of seen characters and then it's the same as nik's solution below. Except he uses for-loop instead of strpos().
Zart