Last active
June 24, 2017 07:34
-
-
Save ram2104/045f1ecdb359a4400141ec7e4c8ada14 to your computer and use it in GitHub Desktop.
Identify word square in O(n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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