[LeetCode By Go 25]389. Find the Difference

题目

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.

解题思路

使用异或的原理

a ^ 0 = a
a ^ a = 0
a ^ b ^ c = a ^ ( b ^ c )

因此,将s,t两个字符串的所有字符转为byte类型进行异或,最后的结果就是多出来的字符

代码

findTheDifference.go

package _389_Find_the_Difference

func FindTheDifference(s string, t string) byte {
    var ret byte
    for _, v := range s {
        ret = ret ^ byte(v)
    }

    for _, v := range t {
        ret = ret ^ byte(v)
    }
    return ret
}

测试

findTheDifference_test.go

package _389_Find_the_Difference

import "testing"

func TestFindTheDifference(t *testing.T) {
    var tests = []struct{
        s string
        t string
        output byte
    }{
        {"abcd","abcde", byte('e')},
    }

    for _, test := range tests {
        ret := FindTheDifference(test.s, test.t)

        if ret == test.output {

        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,980评论 0 23
  • 标签(空格分隔): 编程 Go官方文档 Using the tour 1.1 Hello, 世界 Welcome...
    uangianlap阅读 1,555评论 0 5
  • 艺评新锐“许友维” ——从城坊间、乡土系列人士之“明辉画廊” 火山 在从化的一本土综合网站——神采飞扬网,与许友维...
    朱明云阅读 592评论 2 6
  • 今天是大年初一给大伙儿拜年。 我自己的习惯是每个大年三十夜深人静的时候都要找个地方反思一下过去的一年。 昨天我脑子...
    Curtis2019阅读 169评论 0 0
  • 昨天,周五!美丽周末的开启~可是下班时间倒数时,莫名其妙的被空虚侵袭,内心一万个“浪”在奔腾,可是,孤身一人,何去...
    卿FF阅读 715评论 0 0