hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Slot Game
Problem: Slot Game
Problem Link: https://www.codingninjas.com/codestudio/problems/slot-game_1229394
Approach:
Run a loop through the end of the string.
Check if any of the characters matches both original and the guess.
If matches then changed them into a different character than possible characters. And added 2 with points.
Then checked for the matched character in the other characters of guess for the rest of the characters of the original.
If matches added 1 with points.
Returned the points.
Time and Space Complexity:
Time Complexity: O(1)
Space Complexity: O(1)
Code [C++]
#include <bits/stdc++.h>
int slotScore(string &original, string &guess)
{
int points = 0;
string found = "0000";
for(int i=0; i<4; i++){
if(original[i] == guess[i]){
points += 2;
original[i] = '0';
guess[i] = '0';
}
}
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(original[i] == '0' || guess[j] == '0') continue;
if(original[i] == guess[j]){
original[i] = '0';
guess[j] = '0';
points++;
break;
}
}
}
return points;
}