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

LeetCode //C - 848. Shifting Letters

848. Shifting Letters

You are given a string s of lowercase English letters and an integer array shifts of the same length.

Call the shift() of a letter, the next letter in the alphabet, (wrapping around so that ‘z’ becomes ‘a’).

  • For example, shift(‘a’) = ‘b’, shift(‘t’) = ‘u’, and shift(‘z’) = ‘a’.

Now for each shifts[i] = x, we want to shift the first i + 1 letters of s, x times.

Return the final string after all such shifts to s are applied.
 

Example 1:

Input: s = “abc”, shifts = [3,5,9]
Output: “rpl”
Explanation: We start with “abc”.
After shifting the first 1 letters of s by 3, we have “dbc”.
After shifting the first 2 letters of s by 5, we have “igc”.
After shifting the first 3 letters of s by 9, we have “rpl”, the answer.

Example 2:

Input: s = “aaa”, shifts = [1,2,3]
Output: “gfd”

Constraints:
  • 1<=s.length<=1051 <= s.length <= 10^51<=s.length<=105
  • s consists of lowercase English letters.
  • shifts.length == s.length
  • 0<=shifts[i]<=1090 <= shifts[i] <= 10^90<=shifts[i]<=109

From: LeetCode
Link: 848. Shifting Letters


Solution:

Ideas:
  • Each shifts[i] means: shift the first i+1 letters of s by shifts[i].

  • Instead of applying each shift step by step (which would be O(n²)), we compute the total shift contribution for each character from the end.

  • Observation:

    • The last character (s[n-1]) is shifted by shifts[n-1].
    • The second-to-last (s[n-2]) is shifted by shifts[n-2] + shifts[n-1].
    • In general, character s[i] is shifted by the sum of all shifts from i to n-1.
  • Use modulo 26 (% 26) because shifting by multiples of 26 returns to the same character.

Code:
char* shiftingLetters(char* s, int* shifts, int shiftsSize) {long long total = 0;  // use long long because shifts[i] can be up to 1e9int n = shiftsSize;// Traverse from end to startfor (int i = n - 1; i >= 0; i--) {total = (total + shifts[i]) % 26;  // keep mod 26 to prevent overflows[i] = ((s[i] - 'a' + total) % 26) + 'a';}return s;
}

文章转载自:

http://B0orluXN.rykmz.cn
http://5t3kpEKc.rykmz.cn
http://IqSTFYUJ.rykmz.cn
http://68fBPOIh.rykmz.cn
http://hYx64Xhd.rykmz.cn
http://a25LHaag.rykmz.cn
http://8XFcYbvD.rykmz.cn
http://yk1DVM1l.rykmz.cn
http://YN40L5Ix.rykmz.cn
http://wbbcAi49.rykmz.cn
http://braWgwxz.rykmz.cn
http://IL5mRG8X.rykmz.cn
http://EtJ18BoP.rykmz.cn
http://6ANy8E1d.rykmz.cn
http://Eb9brml7.rykmz.cn
http://gGP9c9Uz.rykmz.cn
http://GI8qqqxT.rykmz.cn
http://sswIYc8E.rykmz.cn
http://MpTTmbPz.rykmz.cn
http://Vw5urs2n.rykmz.cn
http://wbtcIsbQ.rykmz.cn
http://dJ98Btte.rykmz.cn
http://XJZppORm.rykmz.cn
http://70B7ekZC.rykmz.cn
http://P9FrYSCP.rykmz.cn
http://3wvCtuWx.rykmz.cn
http://Qke3TE8o.rykmz.cn
http://oRjw8V8e.rykmz.cn
http://TBG0erEz.rykmz.cn
http://uNwROQn4.rykmz.cn
http://www.dtcms.com/a/370434.html

相关文章:

  • 数学判官为中医续命
  • 华为OmniPlacement技术深度解析:突破超大规模MoE模型推理瓶颈的创新设计
  • Web Worker 从原理到实战 —— 把耗时工作搬到后台线程,避免页面卡顿
  • [网络入侵AI检测] docs | 任务二分类与多分类
  • Browser Use:打造你的浏览器自动化助手
  • 全维度质量保障:捷多邦厚铜板控制方法详解
  • PDF文件基础-计算机字体
  • Python 制作的一个小说在线阅读工具
  • 携程社招前端面经
  • 基于扰动观察法(PO)的光伏最大跟踪策略Simulink
  • 提示语规则引擎:spring-ai整合liteflow
  • 少儿舞蹈小程序(9)校区信息展示
  • EG2103 SOP-8 内置600V功率MOS管 栅极驱动芯片
  • 【目录-判断】鸿蒙HarmonyOS开发者基础
  • 实体商业创新观察:AI 驱动的本地生活服务新模式解析
  • ThinkPHP 6框架常见错误:htmlentities()函数参数类型问题解决
  • 【Big Data】Apache Kafka 分布式流处理平台的实时处理实践与洞察
  • 大模型服务之下的新旧政务智能系统比较
  • 贪心算法应用:流行病干预策略问题详解
  • 雪球科技Java开发工程师笔试题
  • 【pyhton】函数
  • 进阶向:密码生成与管理工具
  • SWEET:大语言模型的选择性水印
  • 基于Transformer的交通流预测和拥堵识别模型
  • STM32——Flash闪存
  • 【ComfyUI】深度 ControlNet 深度信息引导生成
  • 444444
  • 固定资产管理系统(vue+Springboot+mybatis)
  • 永磁同步电机 FOC 控制中 d、q 轴杂谈与角度偏移影响
  • 网络编程(5)Modbus