当前位置: 首页 > news >正文

LeetCode2697. Lexicographically Smallest Palindrome

文章目录

    • 一、题目
    • 二、题解

一、题目

You are given a string s consisting of lowercase English letters, and you are allowed to perform operations on it. In one operation, you can replace a character in s with another lowercase English letter.

Your task is to make s a palindrome with the minimum number of operations possible. If there are multiple palindromes that can be made using the minimum number of operations, make the lexicographically smallest one.

A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Return the resulting palindrome string.

Example 1:

Input: s = “egcfe”
Output: “efcfe”
Explanation: The minimum number of operations to make “egcfe” a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is “efcfe”, by changing ‘g’.
Example 2:

Input: s = “abcd”
Output: “abba”
Explanation: The minimum number of operations to make “abcd” a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is “abba”.
Example 3:

Input: s = “seven”
Output: “neven”
Explanation: The minimum number of operations to make “seven” a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is “neven”.

Constraints:

1 <= s.length <= 1000
s consists of only lowercase English letters.

二、题解

class Solution {
public:string makeSmallestPalindrome(string s) {int n = s.size();int i = 0,j = n - 1;while(i <= j){if(s[i] != s[j]){char c = min(s[i],s[j]);s[i] = c;s[j] = c;}i++;j--;}return s;}
};
http://www.lryc.cn/news/259881.html

相关文章:

  • Leetcode 40 组合总和 II
  • 智慧灯杆技术应用分析
  • 手动搭建koa+ts项目框架(ts项目实现开发阶段实时查看)
  • 在Nexus上配置Docker镜像仓库
  • 深入理解C语言的函数参数
  • 【C++】策略模式
  • 什么时候使用匿名类,匿名类解决了什么问题?为什么需要匿名类 ?
  • 怎么让gpt帮忙改文章 (1) 快码论文
  • Android源码下载流程
  • ArrayList与顺序表(带完整实例)
  • 智能冶钢厂环境监控与设备控制系统(边缘物联网网关)
  • 【Python】conda镜像配置,.condarc文件详解,channel镜像
  • 实战章节:在Linux上部署各类软件
  • 铭飞CMS list 接口 SQL注入漏洞复现
  • Linux指令初始
  • Nginx命令---启动nginx
  • 【UE5】监控摄像头效果(下)
  • binkw32.dll丢失怎么办?这5个方法都可以解决binkw32.dll丢失问题
  • C语言-每日刷题练习
  • Qt设置类似于qq登录页面(ikun)
  • Qt 如何使用VTK显示点云
  • Ganache结合内网穿透实现远程或不同局域网进行连接访问
  • Qt槽函数不响应不执行的一种原因:ui提升导致重名
  • vuepress路径问题,导致图片不显示
  • QT 重定向qdebug输出到自绘界面
  • 前端(一):HTML+CSS
  • 如何使用Matlab完成窗口与子窗口
  • Threejs之相机基础
  • 2024SIA上海国际轴承工业展览会 ▎参行业盛会 展轴研风采
  • SQLMap介绍