Java Solution with Iterative / T : O(N), S:O(N)


  • 0
    J
    public List<String> generatePossibleNextMoves(String s) {
            List<String> lst = new ArrayList<String>();
            char[] chs = s.toCharArray();
            
            for(int i = 0; i < chs.length-1; i++){
                if(chs[i] == '+' && chs[i+1] == '+'){
                    flip(chs, i);
                    lst.add(String.valueOf(chs));
                    flip(chs, i);
                }
            }
            return lst;
        }
        
        public void flip(char[] chs, int i){
            chs[i] = (chs[i] == '-') ? '+' : '-';
            chs[i+1] = (chs[i+1] == '-') ? '+' : '-';
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.