-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlotgame.java
More file actions
37 lines (35 loc) · 1.23 KB
/
Slotgame.java
File metadata and controls
37 lines (35 loc) · 1.23 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
import java.util.*;
public class Slotgame {
public static int slotScore(String original, String guess) {
// Write your code here.
if (guess.length() == 0 || original.length() == 0) {
return 0;
}
int count = 0;
Set<Character> originalChars = new HashSet<>(); // To track used characters
Set<Character> guessChars = new HashSet<>();
for (int i = 0; i < original.length(); i++) {
if (original.charAt(i) == guess.charAt(i)) {
count += 2;
} else {
originalChars.add(original.charAt(i));
guessChars.add(guess.charAt(i));
}
}
for (char c : guessChars) {
if (originalChars.contains(c)) {
count++;
originalChars.remove(c); // Remove the character from originalChars to avoid double counting
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // System.in is a standard input stream
String original = sc.next();
String guess = sc.next();
sc.close();
int ans = slotScore(original, guess);
System.out.println(ans);
}
}