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

LeetCode //C - 389. Find the Difference

389. Find the Difference

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.
 

Example 1:

Input: s = “abcd”, t = “abcde”
Output: “e”
Explanation: ‘e’ is the letter that was added.

Example 2:

Input: s = “”, t = “y”
Output: “y”

Constraints:
  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s and t consist of lowercase English letters.

From: LeetCode
Link: 389. Find the Difference


Solution:

Ideas:
  • The function findTheDifference works by calculating the sum of the ASCII values of characters in both strings s and t.
  • Since t contains all characters from s plus one extra character, the difference between the two sums gives the extra character.
Code:
char findTheDifference(char* s, char* t) {int char_sum_s = 0, char_sum_t = 0;// Sum the ASCII values of all characters in string swhile (*s) {char_sum_s += *s;s++;}// Sum the ASCII values of all characters in string twhile (*t) {char_sum_t += *t;t++;}// The difference in the sums will give the added characterreturn char_sum_t - char_sum_s;
}

http://www.mrgr.cn/news/38786.html

相关文章:

  • 新组合:节律微生态=单菌完成图+宏基因组
  • Maven(1)什么是Maven?
  • 人工打电话的操作步骤指南
  • 平衡操控使用场景分析和对低延迟的直播技术要求
  • 感知红利!单车ADAS摄像头提至9.45颗!市场激战再升级
  • 安全类面试题-填空题
  • Spring Boot 进阶-Spring Boot 如何实现自定义的过滤器详解
  • 人工智能与机器学习原理精解【29】
  • 生信初学者教程(十五):差异结果的热图
  • 聚势启新 智向未来 | 重庆华阳通用科技有限公司揭牌成立
  • PHP基础语法
  • Java SPI 原理、样例
  • 基于Python的人工智能应用案例系列(14):Fashion MNIST图像分类CNN
  • 八段锦之养生功效:AI语义学分析
  • 进程管理工具:非daemon进程管理工具supervisor
  • 物理加密机的高性能操作
  • AutoGen框架进行多智能体协作—AI Agentic Design Patterns with AutoGen(一)
  • 【Linux】修改用户名用户家目录
  • C# Blazor Server 调用海康H5Player播放摄像头画面
  • 带链的队列,入队,退队,检测带链队列的状态