Search code examples
javaalgorithmpalindrome

scanning in and grabbing a specific token


I am having trouble with what I have been testing for a few days now.

All using Stacks and Queues

Sample.txt : My mom and dad both think I will do good at my gig tomorrow.

I have a GUI that attach's a file and parses the file for Palindromes. I want to for instance,

I can find mom as a palindrome, and since mom.length() is == to 3 i would then grab the third token from mom, which in this case would be both. I am able to grab all of these palindromes correctly, just at a loss on how i would traverse tokens that i haven't 'read' in yet?

my method is,

public void fileDecode() throws FileNotFoundException
    {


            while(scanInput.hasNext())
            {
                int counter = 0;
                int nextPalindrome = 0;
                String token = scanInput.next();
                Stack<Character> stk = new Stack<Character>();
                Queue<Character> que = new LinkedList<Character>();
                for (int i = 0; i < token.length(); ++i)
                {
                    stk.push(token.charAt(i));
                    que.add(token.charAt(i));

                }
                for (int j = 0; j < token.length(); ++j)
                {
                        char tempStk = stk.pop();
                        char tempQue = que.remove();

                        if (tempStk == tempQue)
                        {
                            counter++;
                        }
                }

                if (counter == token.length())
                {
                   //build.append(token + " ");  #if i want to see the palindromes
                    nextPalindrome = token.length(); //the length/distance of the token desired
                } 

            }  
        }
    }

Solution

  • just at a loss on how i would traverse tokens that i haven't 'read' in yet?

    You read them and skip them. Just use a local variable which is usually 0, but once you find a palindrome you set it to the palindrome's length. Then before the checking logic you check this variable and continue the loop if it is greater than 0.

    // Idea: there are 3 cases:
    // 1. tokensToSkip is 0: process as normal (check for palindromeness)
    // 2. tokensToSkip is 1: decrement it to 0, but process as normal
    // 3. tokensToSkip is 2, 3, 4...: decrement it and skip (continue the while)
    // Note that tokensToSkip will never be negative.
    
    // Will skip tokensToSkip - 1 tokens
    int tokensToSkip = 0;
    while(scanInput.hasNext()) {
        String token = scanInput.next();
        // instead of the next two lines we could just have:
        //     if (--tokensToSkip > 0) continue;
        // only once, notice the -- operator in front.
        //
        // This would almost always work, but if tokensToSkip = Integer.MIN_VALUE
        // decrementing it will cause negative overflow and thus tokensToSkip will
        // become Integer.MAX_VALUE! To prevent this we have a check.
        if (tokensToSkip > 0) tokensToSkip--;
    
        // here tokensToSkip is already decremented, so this check is different
        // from the one above. For example is tokensToSkip == 1 before the line
        // above, now it is tokensToSkip == 0 and this second check fails.
        if (tokensToSkip > 0) continue;
        if (isPalindrome(token)) {
            tokensToSkip = token.length();
        }
        // ... other stuff ...
    }
    

    By the way, a more efficient way to implement isPalindrome() is:

    static boolean isPalindrome(String s) {
        for (int i = 0; i < s.length() / 2; i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }