find the differences

Question:

Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:

Input:s = "abcd"t = "abcde"
Output:e
Explanation:'e' is the letter that was added.

Solution:

Java:

public char findTheDifference(String s, String t) { 
    int n = t.length(); 
    char c = t.charAt(n - 1); 
    for (int i = 0; i < n - 1; ++i) {       
        c ^= s.charAt(i);
        c ^= t.charAt(i);
    } 
    return c;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,418评论 0 23
  • 他还是不喜欢我,始终都是我自作多情,我会变瘦的,我会逆袭,我会变得更优秀的,或许我们开始就不是一个世界的人,是我错了……
    9322d2314052阅读 1,228评论 3 0
  • 1.什么是组件? A.组件很类似功能模块,但会更细致一点.B.其实不需要纠结单个组件到底是不是以对象实体的形式存在...
    破弓阅读 3,843评论 0 5
  • 上一章 缘分的绳索中,只要有一方使力,另一方必定无法置身事外,很多年后郁唯一才明白,不是缘分太深,而是有人用力太深...
    121跑起来阅读 3,122评论 0 1
  • 笔记本末尾总拖着“张学良回忆录”中西安事变一段,且几个版本,删了,没意思;另一篇《皖南事变是黄桥枪声的报应》太长,...
    Lnrsyao阅读 3,633评论 0 0