49
49
50
50
``` python
51
51
class Solution :
52
- def generatePossibleNextMoves (self , s : str ) -> List[str ]:
53
- if not s or len (s) < 2 :
54
- return []
55
- n = len (s)
56
- res = []
57
- for i in range (n - 1 ):
58
- if s[i] == ' + ' and s[i + 1 ] == ' + ' :
59
- res.append(s[: i] + " -- " + s[i + 2 :])
60
- return res
52
+ def generatePossibleNextMoves (self , currentState : str ) -> List[str ]:
53
+ s = list (currentState)
54
+ ans = []
55
+ for i, c in enumerate (s[: - 1 ]):
56
+ if c == " + " and s[i + 1 ] == " + " :
57
+ s[i] = s[i + 1 ] = " - "
58
+ ans.append( " " .join(s))
59
+ s[ i] = s[i + 1 ] = " + "
60
+ return ans
61
61
```
62
62
63
63
### ** Java**
@@ -66,22 +66,63 @@ class Solution:
66
66
67
67
``` java
68
68
class Solution {
69
- public List<String > generatePossibleNextMoves (String s ) {
70
- int n;
71
- if (s == null || (n = s. length()) < 2 ) return Collections . emptyList();
72
- List<String > res = new ArrayList<> ();
73
- for (int i = 0 ; i < n - 1 ; ++ i) {
74
- if (s. charAt(i) == ' +' && s. charAt(i + 1 ) == ' +' ) {
75
- StringBuilder sb = new StringBuilder (s);
76
- sb. replace(i, i + 2 , " --" );
77
- res. add(sb. toString());
69
+ public List<String > generatePossibleNextMoves (String currentState ) {
70
+ char [] cs = currentState. toCharArray();
71
+ List<String > ans = new ArrayList<> ();
72
+ for (int i = 0 ; i < cs. length - 1 ; ++ i) {
73
+ if (cs[i] == ' +' && cs[i + 1 ] == ' +' ) {
74
+ cs[i] = ' -' ;
75
+ cs[i + 1 ] = ' -' ;
76
+ ans. add(String . valueOf(cs));
77
+ cs[i] = ' +' ;
78
+ cs[i + 1 ] = ' +' ;
78
79
}
79
80
}
80
- return res ;
81
+ return ans ;
81
82
}
82
83
}
83
84
```
84
85
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ vector<string > generatePossibleNextMoves(string currentState) {
92
+ vector<string > ans;
93
+ for (int i = 0; i < currentState.size() - 1; ++i)
94
+ {
95
+ if (currentState[ i] == '+' && currentState[ i + 1] == '+')
96
+ {
97
+ currentState[ i] = '-';
98
+ currentState[ i + 1] = '-';
99
+ ans.push_back(currentState);
100
+ currentState[ i] = '+';
101
+ currentState[ i + 1] = '+';
102
+ }
103
+ }
104
+ return ans;
105
+ }
106
+ };
107
+ ```
108
+
109
+ ### **Go**
110
+
111
+ ```go
112
+ func generatePossibleNextMoves(currentState string) []string {
113
+ ans := []string{}
114
+ cs := []byte(currentState)
115
+ for i, c := range cs[1:] {
116
+ if c == '+' && cs[i] == '+' {
117
+ cs[i], cs[i+1] = '-', '-'
118
+ ans = append(ans, string(cs))
119
+ cs[i], cs[i+1] = '+', '+'
120
+ }
121
+ }
122
+ return ans
123
+ }
124
+ ```
125
+
85
126
### ** ...**
86
127
87
128
```
0 commit comments