|
1 | 1 | package com.fishercoder.solutions;
|
2 |
| -/**Given two strings s and t which consist of only lowercase letters. |
| 2 | +/** |
| 3 | + * 389. Find the Difference |
| 4 | + * |
| 5 | + * Given two strings s and t which consist of only lowercase letters. |
3 | 6 |
|
4 |
| - String t is generated by random shuffling string s and then add one more letter at a random position. |
| 7 | + String t is generated by random shuffling string s and then add |
| 8 | + one more letter at a random position. |
5 | 9 |
|
6 | 10 | Find the letter that was added in t.
|
7 | 11 |
|
|
14 | 18 | Output:
|
15 | 19 | e
|
16 | 20 |
|
17 |
| - Explanation: |
18 |
| - 'e' is the letter that was added.*/ |
| 21 | + Explanation: 'e' is the letter that was added.*/ |
| 22 | + |
19 | 23 | public class _389 {
|
20 |
| - public char findTheDifference(String s, String t) { |
21 |
| - int[] counts = new int[128]; |
22 |
| - char[] schars = s.toCharArray(); |
23 |
| - char[] tchars = t.toCharArray(); |
24 |
| - for (int i = 0; i < schars.length; i++) { |
25 |
| - counts[schars[i]]++; |
26 |
| - } |
27 |
| - for (int i = 0; i < tchars.length; i++) { |
28 |
| - counts[tchars[i]]--; |
29 |
| - } |
30 |
| - char result = 'a'; |
31 |
| - for (int i = 0; i < 128; i++) { |
32 |
| - if (counts[i] != 0) { |
33 |
| - result = (char) i; |
| 24 | + public static class Solution1 { |
| 25 | + public char findTheDifference(String s, String t) { |
| 26 | + int[] counts = new int[128]; |
| 27 | + char[] schars = s.toCharArray(); |
| 28 | + char[] tchars = t.toCharArray(); |
| 29 | + for (int i = 0; i < schars.length; i++) { |
| 30 | + counts[schars[i]]++; |
| 31 | + } |
| 32 | + for (int i = 0; i < tchars.length; i++) { |
| 33 | + counts[tchars[i]]--; |
| 34 | + } |
| 35 | + char result = 'a'; |
| 36 | + for (int i = 0; i < 128; i++) { |
| 37 | + if (counts[i] != 0) { |
| 38 | + result = (char) i; |
| 39 | + } |
34 | 40 | }
|
| 41 | + return result; |
35 | 42 | }
|
36 |
| - return result; |
37 | 43 | }
|
38 | 44 | }
|
0 commit comments