Skip to content

1957. Delete Characters to Make Fancy String #775

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to ensure that no three consecutive characters are the same in the final string. We'll iterate through the input string and build a new "fancy" string by keeping track of the previous two characters. If a third consecutive character matches the last two, we skip it. Otherwise, we add it to the output.

Let's implement this solution in PHP: 1957. Delete Characters to Make Fancy String

<?php
/**
 * @param String $s
 * @return String
 */
function makeFancyString($s) {
    $result = "";
    $n = strlen($s);

    for ($i = 0; $i < $n; $i++) {
        // Check if last two characters in the result are the same as current character
        if ($i > 1 && $s[$i] == $result[strlen($result) - 1]…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Nov 1, 2024
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Nov 1, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 1, 2024
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants