当前位置:网站首页>Huawei machine test question: String transformation minimum string JS
Huawei machine test question: String transformation minimum string JS
2022-07-27 08:58:00 【Big chicken legs are best】
Title Description :
Given a string s, You can only transform once at most , Returns the smallest string that can be obtained after transformation ( Compare in dictionary order ).
Changing rules : Swap characters at any two different positions in the string .
Input description :
A string of lowercase letters s.
Output description :
The minimum string obtained by transforming as required .
remarks :
s Are all lowercase characters
1<=s.length<=1000
Example
Input :abcdef
Output :abcdef
explain :abcdef Is already the smallest string , No exchange required
Input :bcdefa
Output :acdefb
explain :a and b Exchange positions , Can get the minimum string
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)the second
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(''))边栏推荐
- Primary function t1744963 character writing
- Some practical, commonly used and increasingly efficient kubernetes aliases
- NIO this.selector.select()
- MATLAB data import -- importdata and load functions
- How to deploy yolov6 with tensorrt
- User management - restrictions
- Deep understanding of Kalman filter (1): background knowledge
- Network IO summary
- 1.3.1 Full Permutation Problem
- 03.使用引号来监听对象嵌套值的变化
猜你喜欢

Deep understanding of Kalman filter (2): one dimensional Kalman filter

500报错

B tree

“寻源到结算“与“采购到付款“两者有什么不同或相似之处?

How to permanently set source

4279. 笛卡尔树

4276. Good at C

Aruba learning notes 10 security authentication portal authentication (web page configuration)

"Weilai Cup" 2022 Niuke summer multi school training camp 1

View 的滑动冲突
随机推荐
Deep understanding of Kalman filter (3): multidimensional Kalman filter
NIO总结文——一篇读懂NIO整个流程
03. Use quotation marks to listen for changes in nested values of objects
3428. Put apples
4276. 擅长C
B tree
Deep understanding of Kalman filter (1): background knowledge
N queen problem (backtracking, permutation tree)
MySQL Express
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
NiO Summary - read and understand the whole NiO process
博客怎么上传动态gif图
PVT的spatial reduction attention(SRA)
BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans
4279. Cartesian tree
4279. 笛卡尔树
Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises
[interprocess communication IPC] - semaphore learning
2036: [Blue Bridge Cup 2022 preliminary] statistical submatrix (two-dimensional prefix sum, one-dimensional prefix sum)
4274. 后缀表达式