当前位置:网站首页>HUAWEI 机试题:字符串变换最小字符串 js
HUAWEI 机试题:字符串变换最小字符串 js
2022-07-27 08:50:00 【大鸡腿最好吃】
题目描述:
给定一个字符串s,最多只能进行一次变换,返回变换后能得到的最小字符串(按照字典序进行比较)。
变换规则:交换字符串中任意两个不同位置的字符。
输入描述:
一串小写字母组成的字符串s。
输出描述:
按照要求进行变换得到的最小字符串。
备注:
s是都是小写字符组成
1<=s.length<=1000
示例
输入:abcdef
输出:abcdef
说明:abcdef已经是最小字符串,不需要交换
输入:bcdefa
输出:acdefb
说明:a和b进行位置交换,可以得到最小字符串
let a=readline()
let res=a,b=a.split('').sort()
for(let i=0;i<a.length;i++){
if(a[i]!=b[i]){
let p=a.lastIndexOf(b[i])
res=a.slice(0,i)+b[i]+a.slice(i+1,p)+a[i]+a.slice(p+1)
break
}
}
console.log(res)第二个
let a=readline()
let res=a.split('')
for(let i=0;i<a.length;i++){
let p=a[i],t=i
for(let j=i+1;j<a.length;j++){
if(a[j]<p){
p=a[j]
t=j
}
}
if(t!=i){
res[i]=p
res[t]=a[i]
break
}
}
console.log(res.join(''))边栏推荐
- “鼓浪屿元宇宙”,能否成为中国文旅产业的“升级样本”
- 【nonebot2】几个简单的机器人模块(一言+彩虹屁+每日60s)
- NiO example
- Matlab solves differential algebraic equations (DAE)
- Cache consistency and memory barrier
- User management - restrictions
- Day5 - Flame restful request response and Sqlalchemy Foundation
- NIO总结文——一篇读懂NIO整个流程
- 693. 行程排序
- [nonebot2] several simple robot modules (Yiyan + rainbow fart + 60s per day)
猜你喜欢

View 的滑动冲突

无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。

What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?

P7 Day1 get to know the flask framework

Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum

Sliding conflict of view

Unity3D 2021软件安装包下载及安装教程

4274. 后缀表达式

Mmrotate trains its dataset from scratch

vscod
随机推荐
Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum
Flask request data acquisition and response
NIO this.selector.select()
[flutter -- geTx] preparation
【渗透测试工具分享】【dnslog服务器搭建指导】
User management - restrictions
“寻源到结算“与“采购到付款“两者有什么不同或相似之处?
New year's goals! The code is more standardized!
3311. 最长算术
接口测试工具-Postman使用详解
PyQt5快速开发与实战 4.1 QMainWindow
[I2C reading mpu6050 of Renesas ra6m4 development board]
2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)
Flask login implementation
4279. Cartesian tree
Supervisor 安装与使用
Full Permutation (depth first, permutation tree)
NiO example
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
如何在B站上快乐的学习?