package com.rwl.codejam.t9spelling.algorithm;

import com.rwl.codejam.t9spelling.model.T9Spelling;
import com.rwl.codejam.utils.FileReaderCallback;
import java.io.BufferedReader;
import java.io.IOException;

/**
 *
 * @author rainwhileloop
 */
public class Speller implements FileReaderCallback {

    @Override
    public void doing(BufferedReader br) throws IOException {
        int testCase = Integer.parseInt(br.readLine());
        System.out.println("Total test case : " + testCase);
        String input, output;
        int minimalFormatRange = 20;
        int i = 1;
        while ((input = br.readLine()) != null) {

            output = pressButtonFollowText(input);
            System.out.printf("%3d -> %" + (minimalFormatRange - input.length()) + "s|%" + input.length() + "s| : %s\n",
                    i++,
                    "",
                    input,
                    output);
        }
    }

    public String pressButtonFollowText(String text) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            try {
                String current = T9Spelling.valueOf(text.charAt(i));
                if (i == 0) {
                    builder.append(current);
                } else {
                    String previous = String.valueOf(builder.charAt(builder.length() - 1));
                    if (isSameButton(current, previous)) {
                        builder.append(" ");
                    }
                    builder.append(current);
                }
            } catch (IndexOutOfBoundsException ex) {

            }
        }
        return builder.toString();
    }

    private boolean isSameButton(String newText, String previousText) {
        return newText.contains(previousText) || previousText.contains(newText);
    }

}

@@ -1019,24 +1019,66 @@
 gBuilder();%0A
+        Character lastButtonPress = null;%0A
         for 
@@ -1119,30 +1119,8 @@
 ) %7B%0A
-            try %7B%0A    
     
@@ -1188,28 +1188,24 @@
             
-    
 if (i == 0) 
@@ -1214,36 +1214,32 @@
                 
-   
- 
 builder.append(c
@@ -1259,20 +1259,16 @@
         
-    
 %7D else %7B
@@ -1288,147 +1288,72 @@
     
-    
-String previous = String.valueOf(builder.charAt(builder.length() - 1));%0A                    if (isSameButton(current, previou
+if (isSameButton(getLastCharacter(current), lastButtonPres
 s)) %7B%0A
-    
     
@@ -1397,36 +1397,32 @@
                 
-    
 %7D%0A              
@@ -1415,36 +1415,32 @@
                 
- 
-   
 builder.append(c
@@ -1452,36 +1452,32 @@
 t);%0A            
-    
 %7D%0A            %7D 
@@ -1478,63 +1478,63 @@
     
-%7D catch (IndexOutOfBoundsException ex) %7B%0A%0A            %7D
+lastButtonPress = builder.charAt(builder.length() - 1);
 %0A   
@@ -1619,133 +1619,294 @@
 ton(
-String newText, String previousText) %7B%0A        return newText.contains(previousText) %7C%7C previousText.contains(newText);%0A    %7D%0A
+char newBottonPress, char lastButtonPress) %7B%0A        return newBottonPress == lastButtonPress;%0A    %7D%0A%0A    private Character getLastCharacter(String text)%7B%0A        if(text != null && !text.isEmpty())%7B%0A            return text.charAt(text.length() - 1);%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

edit check last press button