Skip to content

Commit bd2c0d8

Browse files
refactor 389
1 parent c0124dd commit bd2c0d8

File tree

1 file changed

+25
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-19
lines changed
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
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.
36
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.
59
610
Find the letter that was added in t.
711
@@ -14,25 +18,27 @@
1418
Output:
1519
e
1620
17-
Explanation:
18-
'e' is the letter that was added.*/
21+
Explanation: 'e' is the letter that was added.*/
22+
1923
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+
}
3440
}
41+
return result;
3542
}
36-
return result;
3743
}
3844
}

0 commit comments

Comments
 (0)