FastPrepReplace '?' to Avoid Adjacent Duplicates
Problems/Replace '?' to Avoid Adjacent Duplicates

Replace '?' to Avoid Adjacent Duplicates

Salesforce logoSalesforce● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a string s consisting of lowercase English letters ('a' to 'z') and the character '?'.

Each '?' can be replaced with any lowercase English letter.

Return the total number of ways to replace all '?' such that the final string has no two adjacent identical characters.

Since the answer may be large, return it modulo 10^9 + 7.

Function

replaceQuestionMarkToAvoidAdjacentDuplicates(s: String) → int

Examples

Example 1

s = "??"return = 650
  • First character: 26 choices
  • Second character: 25 choices (cannot equal the previous character)
  • Total ways = 26 * 25 = 650

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is a lowercase English letter or '?'

More Salesforce Online Assessment questions

  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
  • Minimum Unique-Character Segments After DeletionOA · Seen Sep 2026
  • Run-Length String CompressionOA · Seen Sep 2026
  • Maximum Requests in WindowOA · Seen Sep 2026
  • Final Pod Counts After LogsOA · Seen Aug 2026
  • Minimum Removals to Balance ArrayOA · Seen Aug 2026
  • Is This Date a Weekend?OA · Seen Aug 2026
  • Maximum Barbell WeightOA · Seen Jul 2026

Similar questions at other companies

  • Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
  • Count Equal-Frequency SubsequencesExpedia · OAExpedia interview questions
  • Generate One-Two Strings with Sum NGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Count Vowel PermutationsZscaler · ONSITE INTERVIEWZscaler interview questions
  • Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
  • Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
  • Longest Subsequence That Is a SubstringBooking.com · OA
  • Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
  • Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int replaceQuestionMarkToAvoidAdjacentDuplicates(String s) {
      // write your code here
    }

}
Checking account…