Code is same but working differently

Could someone please tell me why code 1 is giving correct output while code 2 is giving WA. Why is the logic in the last if statement in code 1 is mandatory?

Link to questions - https://www.codechef.com/LRNDSA02/problems/COMPILER

code 1

	while(test-->0)
	{
	    String pattern = br.readLine();
	    long res = 0, count = 0;
	    Stack<Character> st = new Stack<Character>();
	    for(int i=0; i<pattern.length(); i++)
	    {
	        char c = pattern.charAt(i);
	        if(st.isEmpty() && c=='>')
	            break;
	        if(c=='<')
	        {
	            st.push(c);
	            count++;
	        }
	        if(c=='>')
	        {
	            count++;
	            st.pop();
	        }
	        if(st.isEmpty())
	        {
	            res += count;
	            count = 0;
	        }
	    }
	    System.out.println(res);
	}

code 2

	while(test-->0)
	{
	    String pattern = br.readLine();
	    long res = 0, count = 0;
	    Stack<Character> st = new Stack<Character>();
	    for(int i=0; i<pattern.length(); i++)
	    {
	        char c = pattern.charAt(i);
	        if(st.isEmpty() && c=='>')
	            break;
	        if(c=='<')
	        {
	            st.push(c);
	            count++;
	        }
	        if(c=='>')
	        {
	            count++;
	            st.pop();
	        }
	        
	    }
	    System.out.println(count);
	}

Without giving the link or description of question how someone can predict that res += count is required or not, definitely count = 0 is of no use. So give some details about it.

https://www.codechef.com/LRNDSA02/problems/COMPILER. Here it is

Check the output for <<

output = 0 in code 1. How is it possible. It should be 2, i think

got it. thanks bro.

Why do you think o/p should be 2? Read question again. ( For each expression you should output the length of the longest prefix that is valid or 0 if there’s no such a prefix.)

i got it. thanks again,.