-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-1275.cpp
49 lines (44 loc) · 1.36 KB
/
Problem-1275.cpp
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
//Problem - 1275
// https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/
// O(moves.size()) time complexity and O(1) space complexity
class Solution {
public:
bool isWinner(vector <string> &grid) {
int ctr = 0, ctr1 = 0;
for(int i = 0; i < 3; i++) {
ctr = 0, ctr1 = 0;
char c1 = grid[i][0], c2 = grid[0][i];
for(int j = 0; j < 3; j++) {
if(grid[i][j] == c1 && c1 != '$')
ctr++;
if(grid[j][i] == c2 && c2 != '$')
ctr1++;
}
if(ctr == 3 || ctr1 == 3)
return 1;
}
ctr = ctr1 = 0;
char c1 = grid[0][0], c2 = grid[2][0];
for(int i = 0, j = 0; i < 3; i++, j++) {
if(grid[i][j] == c1 && c1 != '$')
ctr++;
if(grid[i][2-j] == c2 && c2 != '$')
ctr1++;
}
if(ctr == 3 || ctr1 == 3)
return 1;
return 0;
}
string tictactoe(vector<vector<int>>& moves) {
vector <string> grid(3, "$$$");
int i = 0;
for(i = 0; i < moves.size(); i++) {
grid[moves[i][0]][moves[i][1]] = i&1 ? 'B' : 'A';
if(isWinner(grid))
return (i&1 ? "B" : "A");
}
if(i == 9)
return "Draw";
return "Pending";
}
};