zoukankan      html  css  js  c++  java
  • 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.Input:

    解题思路:用字典存储每个字母的出现次数


    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using Algorithm;
    6. namespace Solution {
    7. public class Solution {
    8. public char FindTheDifference(string s, string t) {
    9. var d = new Dictionary<char, int>();
    10. foreach (var c in s) {
    11. if (d.ContainsKey(c)) d[c]++;
    12. else d[c] = 1;
    13. }
    14. foreach (var c in t) {
    15. if (d.ContainsKey(c)) d[c]++;
    16. else d[c] = 1;
    17. }
    18. foreach (var i in d.Keys) {
    19. if (d[i] % 2 != 0) {
    20. return i;
    21. }
    22. }
    23. return ' ';
    24. }
    25. }
    26. class Program {
    27. static void Main(string[] args) {
    28. Solution s = new Solution();
    29. var res = s.FindTheDifference("abcdef", "abcde");
    30. Console.WriteLine(res);
    31. }
    32. }
    33. }






  • 相关阅读:
    java学习笔记(5)
    java学习笔记(4)
    java学习笔记(3)
    java学习笔记(2)
    java学习笔记(1)
    很棒的Nandflash资料
    Tx2440_Lcd
    git-github学习心得
    多文档编辑器
    假设检验
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/d3288964e2913d2692a24fe836d516b5.html
Copyright © 2011-2022 走看看