Class Solution

java.lang.Object
g1901_2000.s1927_sum_game.Solution

public class Solution extends Object
1927 - Sum Game.<p>Medium</p> <p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>You are given a string <code>num</code> of <strong>even length</strong> consisting of digits and <code>'?'</code> characters. On each turn, a player will do the following if there is still at least one <code>'?'</code> in <code>num</code>:</p> <ol> <li>Choose an index <code>i</code> where <code>num[i] == '?'</code>.</li> <li>Replace <code>num[i]</code> with any digit between <code>'0'</code> and <code>'9'</code>.</li> </ol> <p>The game ends when there are no more <code>'?'</code> characters in <code>num</code>.</p> <p>For Bob to win, the sum of the digits in the first half of <code>num</code> must be <strong>equal</strong> to the sum of the digits in the second half. For Alice to win, the sums must <strong>not be equal</strong>.</p> <ul> <li>For example, if the game ended with <code>num = &quot;243801&quot;</code>, then Bob wins because <code>2+4+3 = 8+0+1</code>. If the game ended with <code>num = &quot;243803&quot;</code>, then Alice wins because <code>2+4+3 != 8+0+3</code>.</li> </ul> <p>Assuming Alice and Bob play <strong>optimally</strong> , return <code>true</code> <em>if Alice will win and</em> <code>false</code> <em>if Bob will win</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = &ldquo;5023&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> There are no moves to be made. The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = &ldquo;25??&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> Alice can replace one of the &rsquo;?&rsquo;s with &lsquo;9&rsquo; and it will be impossible for Bob to make the sums equal.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> num = &ldquo;?3295???&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> It can be proven that Bob will always win. One possible outcome is:</p> <ul> <li> <p>Alice replaces the first &lsquo;?&rsquo; with &lsquo;9&rsquo;. num = &ldquo;93295???&rdquo;.</p> </li> <li> <p>Bob replaces one of the &lsquo;?&rsquo; in the right half with &lsquo;9&rsquo;. num = &ldquo;932959??&rdquo;.</p> </li> <li> <p>Alice replaces one of the &lsquo;?&rsquo; in the right half with &lsquo;2&rsquo;. num = &ldquo;9329592?&rdquo;.</p> </li> <li> <p>Bob replaces the last &lsquo;?&rsquo; in the right half with &lsquo;7&rsquo;. num = &ldquo;93295927&rdquo;.</p> </li> </ul> <p>Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= num.length <= 10<sup>5</sup></code></li> <li><code>num.length</code> is <strong>even</strong>.</li> <li><code>num</code> consists of only digits and <code>'?'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sumGame

      public boolean sumGame(String num)