Problem · String
Replace '?' to Avoid Adjacent Duplicates
Learn this problemProblem 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) → intExamples
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^5s[i]is a lowercase English letter or'?'
More Salesforce problems
- Diameter of an Acyclic Undirected GraphONSITE INTERVIEW · Seen Jul 2026
- Optimal Account BalancingPHONE SCREEN · Seen Jul 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Jul 2026
- Maximal SquarePHONE SCREEN · Seen Jul 2026
- Maximum Barbell WeightOA · Seen Jul 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Jul 2026
- Minimum Operations to ZeroOA · Seen Jul 2026
- Minimize Total Input Cost (for LTMS)OA · Seen Jun 2026