-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path389FindTheDifference.java
More file actions
50 lines (35 loc) · 1.14 KB
/
389FindTheDifference.java
File metadata and controls
50 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
*/
class Solution {
public char findTheDifference(String s, String t)
{
String tCut = t.substring(0, t.length()-1);
char[] sChars = s.toCharArray();
char[] tChars = t.toCharArray();
Arrays.sort(sChars);
Arrays.sort(tChars);
StringBuilder sBuilt = new StringBuilder();
StringBuilder tBuilt = new StringBuilder();
sBuilt.append(sChars);
tBuilt.append(tChars);
System.out.println(sBuilt);
System.out.println(tBuilt);
boolean found = false; //false until found
for(int i = 0; i < s.length(); i++)
{
if(sChars[i] != tChars[i])
{
return tChars[i];
}
}
if(!found)
{
return tChars[tChars.length-1];
}
return ' ';
//System.out.println("good");
}
}