Leetcode PHP problem solving -- d49 821. Short distance to a character

Keywords: PHP

D49 821. Shortest Distance to a Character

Title Link

821. Shortest Distance to a Character

Title Analysis

Given a string s and a character c.

Returns the minimum distance from each character in the string to the given character c.

thinking

First, find the position of character C in string S with array_keys.

If the current traversal position is before the next character C, then directly subtract the subscript to get the distance.

Otherwise, when the current subscript is greater than the position where the last character C appears and the next character C exists, the distance is the smallest of the two.
When the distance is 0, mark the location of the next C to get.

Final code

<?php
class Solution {
    function shortestToChar($S, $C) {
            $S = str_split($S);
                    $keys = array_keys($S,$C);
                            $distances = [];
                                    $prev = 0;
                                            foreach($S as $index => $char){
                                                        $dist = abs($keys[$prev] - $index);
                                                                    if($index > $keys[$prev] && isset($keys[$prev+1])){
                                                                                    $dist = min($index-$keys[$prev],$keys[$prev+1]-$index);
                                                                                                    if($dist == 0){
                                                                                                                        $prev++;
                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                                $distances[] = $dist;
                                                                                                                                                                        }
                                                                                                                                                                        
                                                                                                                                                                                return $distances;
                                                                                                                                                                                    }
                                                                                                                                                                                    }
                                                                                                                                                                                
                                                                                                                                                                                If you think this article is useful to you, please use [love power generation] (https://afdian.net/@skys215) to help.
                                                                                                                                                                                
                                                                                                                                                                                

Posted by tmallen on Mon, 18 Nov 2019 12:29:32 -0800