package com.fishercoder.solutions;

/**
 * 481. Magical String
 *
 * A magical string S consists of only '1' and '2' and obeys the following rules:

 The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.

 The first few elements of string S is the following: S = "1221121221221121122……"

 If we group the consecutive '1's and '2's in S, it will be:

 1 22 11 2 1 22 1 22 11 2 11 22 ......

 and the occurrences of '1's or '2's in each group are:

 1 2	2 1 1 2 1 2 2 1 2 2 ......

 You can see that the occurrence sequence above is the S itself.

 Given an integer N as input, return the number of '1's in the first N number in the magical string S.

 Note: N will not exceed 100,000.

 Example 1:
 Input: 6
 Output: 3
 Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3.
 */
public class _481 {

    /**credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers
     * Algorithm:

     1. Create an int array a and initialize the first 3 elements with 1, 2, 2.
     2. Create two pointers head and tail. head points to the number which will be used to generate new numbers.
     tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n.
     3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2.
     4. A trick to flip number back and forth between 1 and 2: num = num ^ 3*/
    public int magicalString(int n) {
        if (n <= 0) {
            return 0;
        }
        if (n <= 3) {
            return 1;
        }

        int[] a = new int[n + 1];
        a[0] = 1;
        a[1] = 2;
        a[2] = 2;

        int head = 2;
        int tail = 3;
        int num = 1;
        int result = 1;

        while (tail < n) {
            for (int i = 0; i < a[head]; i++) {
                a[tail] = num;
                if (num == 1 && tail < n) {
                    result++;
                }
                tail++;
            }
            num = num ^ 3;
            head++;
        }

        return result;
    }

}

@@ -932,19 +932,71 @@
  %7B%0A%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 credit: 
@@ -1090,16 +1090,20 @@
 ointers%0A
+    
      * A
@@ -1112,21 +1112,37 @@
 orithm:%0A
-%0A
     
+     *%0A         *
  1. Crea
@@ -1213,16 +1213,22 @@
  2.%0A    
+     *
  2. Crea
@@ -1328,24 +1328,30 @@
 umbers.%0A    
+     *
  tail points
@@ -1455,16 +1455,22 @@
  n.%0A    
+     *
  3. Need
@@ -1590,16 +1590,22 @@
  2.%0A    
+     *
  4. A tr
@@ -1668,19 +1668,33 @@
  num %5E 3
-*/%0A
+%0A         */%0A    
     publ
@@ -1723,32 +1723,36 @@
 nt n) %7B%0A        
+  
+  
 if (n %3C= 0) %7B%0A  
@@ -1745,24 +1745,28 @@
  (n %3C= 0) %7B%0A
+    
             
@@ -1775,34 +1775,42 @@
 turn 0;%0A        
-%7D%0A
+    %7D%0A    
         if (n %3C=
@@ -1823,24 +1823,28 @@
             
+    
 return 1;%0A  
@@ -1841,35 +1841,43 @@
 turn 1;%0A        
+   
+ 
 %7D%0A%0A
+    
         int%5B%5D a 
@@ -1902,16 +1902,20 @@
         
+  
+  
 a%5B0%5D = 1
@@ -1912,24 +1912,28 @@
   a%5B0%5D = 1;%0A
+    
         a%5B1%5D
@@ -1946,16 +1946,20 @@
         
+ 
+   
 a%5B2%5D = 2
@@ -1961,16 +1961,20 @@
 %5D = 2;%0A%0A
+    
         
@@ -1991,24 +1991,28 @@
  2;%0A        
+    
 int tail = 3
@@ -2017,24 +2017,28 @@
  3;%0A        
+   
+ 
 int num = 1;
@@ -2034,24 +2034,28 @@
 nt num = 1;%0A
+    
         int 
@@ -2075,16 +2075,20 @@
         
+  
+  
 while (t
@@ -2090,32 +2090,36 @@
 le (tail %3C n) %7B%0A
+    
             for 
@@ -2166,16 +2166,20 @@
         
+    
 a%5Btail%5D 
@@ -2185,16 +2185,20 @@
  = num;%0A
+    
         
@@ -2253,16 +2253,20 @@
         
+    
 result++
@@ -2263,24 +2263,28 @@
   result++;%0A
+    
             
@@ -2305,16 +2305,20 @@
         
+    
 tail++;%0A
@@ -2321,34 +2321,42 @@
 ++;%0A            
+  
-%7D%0A
+  %7D%0A    
             num 
@@ -2382,16 +2382,20 @@
     
+    
 head++;%0A
     
@@ -2386,24 +2386,28 @@
     head++;%0A
+    
         %7D%0A%0A 
@@ -2409,24 +2409,28 @@
  %7D%0A%0A        
+    
 return resul
@@ -2432,16 +2432,26 @@
 result;%0A
+        %7D%0A
     %7D%0A%0A%7D

refactor 481