Leetcode Flip Game II

Source: Internet
Author: User

You is playing the following Flip Game with your Friend:given a string this contains only these both characters: an + D - , you and your friend take turns to the flip twoconsecutive into "++" "--" . The game ends when a person can no longer make a move and therefore the other person would be the the winner.

Write a function to determine if the starting player can guarantee a win.

For example, given s = "++++" , return true. The starting player can guarantee a win by flipping the middle to "++" become "+--+" .

Follow up:
Derive your algorithm ' s runtime complexity.

Problem Solving Ideas:

Backtracking

Java Code

 Public class Solution {    publicboolean  Canwin (String s) {        for (int i = 0; I < S.length ()-1; i++) {            if(S.charat (i) = = ' + ' && s.charat (i+1) = = ' + ' &&!canwin (s.substring ( 0,i) + "--" + s.substring (i+2))                {returntrue;            }        }         return false ;    }}

Reference:

1. Https://leetcode.com/discuss/64330/4-line-java-solution

2. Https://leetcode.com/discuss/64357/memoization-3150ms-130ms-44ms-python

Leetcode Flip Game II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.