Search for characters in a string that occurs only once

I am writing an algorithm in PHP to solve this Sudoku puzzle. I created an object-oriented implementation with two classes: a class Squarefor each individual fragment on the 9x9 board and a class Sudokuthat has a matrix Squarefor representing the board.

The implementation of the algorithm that I use is a kind of triple level. The first step, which will solve only the most basic puzzles (but the most effective), is to fill in any squares that can take only one value based on the initial setup of the board and accordingly adjust the restrictions on the rest of the unresolved squares.

Usually this process of “constant distribution” does not completely resolve the issue, but it does solve a significant part. The second level will then begin. This analyzes each unit (or 9 squares, each of which must have unique number assignments, such as a row or column) for the “possible” values ​​of each unresolved square. This list of possible values ​​is presented as a string in the class Square:

class Square {

private $name;                // 00, 01, 02, ... , 86, 87, 88
private $peers;               // All squares in same row, col, and box
private $number;              // Assigned value (0 if not assigned)
private $possibles;           // String of possible numbers (1-9)

public function __construct($name, $p = 0) {
  $this->name = $name;
  $this->setNumber($p);
  if ($p == 0) {
    $this->possibles = "123456789";
  }
}

// ... other functions

( ), "" . - , . , , .

: , ? , , 1-9, 1 , , 1, , , - 27 .

+3
6

, " ", , :

$all_possibilities = "1234567891234";
$unique = array();
foreach (count_chars($all_possibilities, 1) as $c => $occurrences) {
  if ($occurrences == 1)
    $unique[] = chr($c);
}
print join("", $unique) . "\n";

: "56789"

+3

"", , AND, OR, XOR, , , .

. "2" "3", 000000110, .

uniques:

$seenonce = 0;
$seenmore = 0;
foreach(all_possibles_for_this_unit as $possibles) {
    $seenmore |= ($possibles & $seenonce);
    $seenonce |= $possibles;
}
$seenonce ^= $seenmore;
if ($seenonce) {
    //something was seen once - now it must be located
}

, , .

+1
 function singletonsInString($instring) {

    $results = array();

    for($i = 1; $i < 10; $i++) {

        $first_pos = strpos($instring, str($i));
        $last_pos = strrpos($instring, str($i));

        if ( $first_pos !== FALSE and $first_pos == $last_pos ) 
            $results[] = $i;

    }

    return $results;

 }

. , FALSE ( ), .

,

 $istr = str($i);
 if ( ($first = strpos($instring, $istr)) !== FALSE 
       and $first == $strrpos($instring, $istr) ) $results[] = $i;

. , , PHP- strpos - , , , .

0

, Sudoku, "Run". Run , col 3x3. , . Run , . . 80% 60% . , , . , , .

- . , , . 4- 18 .

0

, . (= ) ( , ).

, , 2

"1" = 2^0 = 1 =  000000001
"2" = 2^1 = 2 =  000000010
"3" = 2^2 = 4 =  000000100
"4" = 2^3 = 8 =  000001000
... etc up to 
"9" = 2^8 = 256= 100000000

" ", , 3x3 , :

// shows the possibles for 3x3 square number 1 (00-22)
$sum=0;
for ($i=0; $i< 3; $i++)
  for ($j=0; $j < 3; $j++)
         $sum += $square["${i}${j}"]->number

$possibles = $sum ^ 511  // ^ stands for bitwise XOR and 511 is binary 11111111

$possibles "1" , , , :

. :

$possibles1 = 146 // is binary 100100101, 
                 //indicating that this row or 3x3 square has place for "9", "6", "3" and "1"
$possibles2 = 7 //   is binary 000000111, indicating it has place for "3", "2" and "1".

// so:
$possibles1 & $possibles2 
// bitwise AND, will show binary 101 saying that "3" and "1" is unfilled in both bloces
$possibles1 | $possibles2 
// bitwise OR will give that in total it is possible to use "9", "6", "3", "2" and "1" in those two squares together
0
source

Here's a way to use only the PHP built-in functions, which should be pretty fast.

function getUniques($sNumbers)
{
    return join(array_keys(array_count_values(str_split($sNumbers)),1));
}

echo getUniques("1234567891234"); // return 56789;
0
source

All Articles