function check = boundary_word_check ( s )

%*****************************************************************************80
%
%% boundary_word_check() checks a boundary word.
%
%  Discussion:
%
%    A valid boundary word must contain an even number of entries, with
%    equal numbers of 'u' and 'd', and equal numbers of 'l' and 'r'.
%
%    The pairs 'lr', 'rl', 'ud' and 'du' must not occur.
%
%  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:
%
%    boolean check: true if s represent a valid string.
%
  s_len = length ( s );
  s = lower ( s );

  for i = 1 : s_len - 1
    if ( i < s_len )
      t = s(i:i+1);
    else
      t(1) = s(s_len);
      t(2) = s(1);
    end
    if ( t == "ud" || t == "du" || t == "lr" || t == "rl" )
      check = false;
      return
    end
  end

  v = 0;
  h = 0;
  for i = 1 : s_len
    if ( s(i) == 'u' )
      v = v + 1;
    elseif ( s(i) == 'd' )
      v = v - 1;
    elseif ( s(i) == 'l' )
      h = h - 1;
    elseif ( s(i) == 'r' )
      h = h + 1;
    else
      check = false;
      return
    end
  end

  check = ( ( v == 0 ) && ( h == 0 ) );

  return
end
