当前位置:网站首页>LeetCode-1576. Replace all question marks
LeetCode-1576. Replace all question marks
2022-06-12 06:22:00 【Border wanderer】
Here's a small alphabet and '?' String of characters s, Please put all the '?' Convert to a number of lowercase letters , So that the final string does not contain any Keep repeating The characters of .
Be careful : you You can't Modify non '?' character .
Topic test cases guarantee except '?' character outside , There are no consecutive repeated characters .
At the completion of all transformations ( There may be no need to convert ) After the return of the final string . If there are multiple solutions , Please return to any of them . Can prove that , Under given constraints , The answer is always there .
Example 1:
Input :s = "?zs"
Output :"azs"
explain : This example has 25 Kind of solution , from "azs" To "yzs" All of them meet the requirements of the title . Only "z" It's an invalid modification , Because strings "zzs" There are two consecutive repetitions in 'z' .
Example 2:
Input :s = "ubv?w"
Output :"ubvaw"
explain : This example has 24 Kind of solution , Only replace it with "v" and "w" Does not meet the requirements of the topic . because "ubvvw" and "ubvww" All contain characters that repeat continuously .
Example 3:
Input :s = "j?qg??b"
Output :"jaqgacb"
Example 4:
Input :s = "??yw?ipkj?"
Output :"acywaipkja"
Tips :
1 <= s.length <= 100
s Contains only lower case letters and '?' character
#include<iostream>
using namespace std;
class Solution {
public:
string modifyString(string s) {
if (s[0] == '?') {
s[0] = getonechar(s[1]);
}
for (int i = 1; i < s.length() - 1; i++) {
if (s[i] == '?') {
s[i] = getonechar(s[i - 1], s[i + 1]);
}
}
if (s[s.length() - 1] == '?') {
s[s.length() - 1] = getonechar(s[s.length() - 2]);
}
return s;
}
char getonechar(char a) {
for (char i = 'a'; i <= 'z'; i++) {
if (i != a) {
return i;
}
}
return a;
}
char getonechar(char a, char b) {
for (char i = 'a'; i <= 'z'; i++) {
if (i != a && i != b) {
return i;
}
}
return a;
}
};
边栏推荐
- The unity3d script searches for colliders with overlaps within the specified radius
- Sqlite Cross - compile Dynamic Library
- . Net core - pass Net core will Net to cross platform
- Unity surface shader with template buffer
- leetcode 704. Binary search
- Cause analysis of motion blur / smear caused by camera shooting moving objects
- MNIST handwritten data recognition by CNN
- leetcode 35. Search insert location
- PHP 开发环境搭建及数据库增删改查
- Leetcode-93. Restore IP address
猜你喜欢

Getting started with houdininengine HDA and UE4

MNIST handwritten data recognition by CNN

. Net core - pass Net core will Net to cross platform

Touch screen setting for win7 system dual screen extended display

QT--实现TCP通信

Bulk Rename Utility

Redis problem (I) -- cache penetration, breakdown, avalanche

Leetcode January 12 daily question 334 Increasing ternary subsequence

Multithreading (4) -- no lock (2) -- Atomic related atomic classes

关于 Sensor flicker/banding现象的解释
随机推荐
Unity C script implements AES encryption and decryption
RNN implementation regression model
夜神模擬器adb查看log
How do I get the date and time from the Internet- How to get DateTime from the internet?
Multithreading (V) -- concurrency tools (I) -- thread pool (II) -- related contents of ThreadPoolExecutor
[reinstall system] 01 system startup USB flash disk production
On the normalization of camera rotation interpolation
Redis application (I) -- distributed lock
RNN model
Nocturnal simulator ADB view log
zip 和.items()区别
Cause analysis of motion blur / smear caused by camera shooting moving objects
C # converts the hexadecimal code form of text to text (ASCII)
How to increase heap size of JVM [duplicate] - how to increase heap size of JVM [duplicate]
Multithreading Foundation (XI) -- prevent CPU from occupying 100%
The first principle of thinking method
Leetcode-139. Word splitting
leetcode 704. Binary search
The unity3d script searches for colliders with overlaps within the specified radius
Unity3d display FPS script