FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/293_Flip_Game.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
293_Flip_Game.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
24 lines (20 loc) · 913 Bytes
Breadcrumbs
leetcode
/
java
/
293_Flip_Game.java
Copy path
File metadata and controls
24 lines (20 loc) · 913 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
https://leetcode.com/problems/flip-game/
You are playing the following Flip Game with your friend: Given a string that contains only these two
characters: + and -, you and your friend take turns to flip two consecutive "++" into "--".
The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to compute all possible states of the string after one valid move.
Check for consecutive pair of '+' chars.
Time - O(n**2)
Space - O(n**2)
*/
public
class
Solution
{
public
List
<
String
>
generatePossibleNextMoves
(
String
s
) {
List
<
String
>
nextMoves
=
new
ArrayList
<
String
>();
for
(
int
i
=
1
;
i
<
s
.
length
(); ++
i
) {
if
(
s
.
charAt
(
i
-
1
) ==
'+'
&&
s
.
charAt
(
i
) ==
'+'
)
// compare primitives with ==
nextMoves
.
add
(
s
.
substring
(
0
,
i
-
1
) +
"--"
+
s
.
substring
(
i
+
1
));
}
return
nextMoves
;
}
}
Back
|
FazBrowse Home
|
New Git URL