function parity = boundary_word_parity ( s )

%*****************************************************************************80
%
%% boundary_word_parity() returns the parity of a polyomino defined by a boundary word.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 May 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    char s(*): a string of the characters 'u', 'd', 'l' and 'r' that should
%    represent a path around the boundary of a polyomino.
%
%  Output:
%
%    integer parity: the parity of the polyomino.
%
  check = boundary_word_check ( s );

  if ( ~ check )
    parity = NaN;
    return
  end

  s_len = length ( s );
  s = lower ( s );

  bw = zeros ( 2, 1 );

  for i = 1 : s_len

    if ( i == 1 )
      if ( s(i) == 'u' || s(i) == 'r' )
        color = 1;
      else
        color = 2;
      end
    elseif ( s(i) == s(i-1) )
      color = 3 - color;
    end
    bw(color) = bw(color) + 1;
  end

  parity = ( bw(1) - bw(2) ) / 4;

  return
end
