How can I grab the index of where the longest identical consecutive portion of a string begins

luvs2spooge

I'd like the code to output index 6 since d is the starting point in terms of the longest identical consecutive portion in the string.

Not sure what I'm doing wrong here but it's currently returning 3 instead. Seems like I'm going in the right direction but something is missing but I can't pinpoint what.

Any feedback is appreciated! :)

$str = "abbcccddddcccbba";
$array = preg_split('/(.)(?!\1|$)\K/', $str);
$lengths = array_map('strlen', $array);
$maxLength = max($lengths);
$ans = array_search($maxLength, $lengths); // returns 3 but need it to return 6

echo $ans;
nice_dev
$lengths = array_map('strlen', $array);

Above line has only lengths of adjacent similar characters. array_search on max of those lengths will only yield the index where the maximum length is stored. It is totally unrelated with getting the index 6 of your string. If you still wish to get it, you will have to array_sum till that index to get the start index in the actual string.

Snippet:

<?php

$str = "abbcccddddcccbba";

$array = preg_split('/(.)(?!\1|$)\K/', $str);

$lengths = array_map('strlen', $array);

$maxLength = max($lengths);

array_splice($lengths,array_search($maxLength, $lengths));

$ans = array_sum($lengths);

echo $ans;

Online Demo


Alternate Solution:

I would write a simple for loop that uses 2 pointers to keep track of start index of similar characters and record the frequency and start index whenever it is greater than max frequency.

Snippet:

<?php

$str = "abbcccddddcccbba";

$len = strlen($str);

$maxF = 1;
$maxIdx = $startIdx = 0;

for($i = 1; $i < $len; ++$i){
  if($str[ $i ] != $str[ $i - 1] || $i === $len - 1){
     if($str[ $i ] === $str[ $i - 1] && $i === $len - 1) $i++;
     if($maxF < $i - $startIdx){
       $maxF = $i - $startIdx;
       $maxIdx = $startIdx;
     }
     $startIdx = $i;
  }
}

echo $maxIdx;

Online Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I grab the last portion of a log string and interpret it as json?

How can I search for a Regular Expression match that begins before a certain index of a string?

AWK: how can I tell where column begins

How can I grab the index with JS?

How can I grab words in between consecutive tokens via regex?

Grab a portion of List<string>

In Bash, how can I check if a string begins with some value?

How can I grep for a string that begins with a dash/hyphen?

How can I remove a parenthesis that begins with a specific string using python?

how can i grab the reqnum from the string?

How can I extract the unmatched portion of a string in R with regular expressions?

How can I use regex to remove a very specific portion of a string:

How can I print the longest number in a string?

How to display a portion of a string at an index of a string array

Find the index of a string that begins with

Can I change the font color on a portion of a string?

How can I change my regex to eliminate consecutive identical special characters?

How can i grab specific string inside the brackets

How can I grab the last element of a List of string arrays?

How can I grab a group of words from a string?

How can I grab all selectors that are similar to a string in Puppeteer?

PHP: If a string begins with "20 ", "22 ", "24 "or "28 " how can i cut it of?

How can I convert sperated date string to consecutive date string

First index of longest ordered portion of a vector

How can I focus on a certain portion of GoogleMap

APL - How can I find the longest word in a string vector?

How can I return an object with the longest string in a list of dictionaries? (python)

How can I print an string without the longest word

How I can optimize query with where index?