当前位置:网站首页>Huawei interview question: no palindrome string
Huawei interview question: no palindrome string
2022-07-02 14:54:00 【Cool of Si Ku Quan Shu】
subject
【 No palindrome string 】
Definition of palindrome string : Read and reverse the same string
There is already a string that does not contain a palindrome string , The characters of the string are in front of the English letter N individual , And the string does not contain any length greater than or equal to 2 The palindrome of ; Please find the next dictionary order that does not contain palindromes 、 Characters are in front of English letters N individual 、 String with the same length . If it doesn't exist , Please export NO.
Input description :
The first line has an integer :N(1<=N<=26), Indicates that each character range of the string is before N English letters of .
Enter a string in the second line ( Input length <=10000), The input ensures that this string is legal and does not contain a palindrome string .
Output description :
Output the next dictionary order without palindrome string 、 Characters are in front of English letters N individual 、 String with the same length ; If it doesn't exist , Please export ”NO”.
Example 1:
Input :
3
cba
Output :
NO
Example 2:
Input :
4
dbad
Output :
dbcd
Code
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string str;
cin >> str;
int len = str.size();
if(len == 1) {
cout << "NO" << endl;
return 0;
}
for(int i = len - 1; i >= 0; --i){
char next = str[i] + 1;
while(next < 'a'+ n){
int flag = 0;
for(int j = i - 2; j <= i + 2; ++j){
if(j < 0) continue;
if(j >= len) continue;
if(j == i) continue;
if(str[j] == next){
flag = 1;
break;
}
}
if(flag){
next = next + 1;
}else{
str[i] = next;
cout << str << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
边栏推荐
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- Fabric.js 橡皮擦的用法(包含恢复功能)
- Delete element (with transition animation)
- Simple verification code generator for 51 single chip microcomputer experiment
- Kityformula editor configure font size and spacing
- Fabric. JS free draw circle
- Large top heap, small top heap and heap sequencing
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
- STM32标准固件库函数名记忆(二)
- Convolutional neural network (Introduction)
猜你喜欢
Kityformula editor configure font size and spacing
What is erdma? Popular science cartoon illustration
kityformula-editor 配置字号和间距
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
Quick analysis: easy to share the Internet
Fabric.js 缩放画布
Method of creating linked server for cross server data access
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
C语言习题---(数组)
随机推荐
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
kityformula-editor 配置字号和间距
Introduction to mathjax (web display of mathematical formulas, vector)
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
Fabric.js 缩放画布
Quick analysis: easy to share the Internet
【NOI模拟赛】伊莉斯elis(贪心,模拟)
由粒子加速器产生的反中子形成的白洞
871. 最低加油次数 : 简单优先队列(堆)贪心题
电脑怎么设置扬声器播放麦克风的声音
Design and implementation of car query system based on php+mysql
Tmall product details interface (APP, H5 end)
Fabric. JS dynamically set font size
Convolutional neural network (Introduction)
LeetCode 2320. 统计放置房子的方式数
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
C#代码审计实战+前置知识
C# richTextBox控制显示最大行数
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te