Skip to content

Instantly share code, notes, and snippets.

@ram2104
Last active June 24, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ram2104/045f1ecdb359a4400141ec7e4c8ada14 to your computer and use it in GitHub Desktop.
Save ram2104/045f1ecdb359a4400141ec7e4c8ada14 to your computer and use it in GitHub Desktop.
Identify word square in O(n)
var line = "BALL AREA LEAD LADY";
var wordAry = line.split(" ");
console.log(wordAry);
var rowIdx = 0;
var colIdx = wordAry.length;
var len = colIdx;
var isWordSquare = true;
while(colIdx > 0 && rowIdx < len){
if(wordAry[rowIdx][len - colIdx] != wordAry[len-colIdx][rowIdx]){
console.log("Not a word square");
isWordSquare = false;
break;
} else {
--colIdx;
}
if(len - colIdx == len){
++rowIdx;
colIdx = len;
}
}
if(isWordSquare){ console.log("word square is it."); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment