Simple Swift solution


  • 0
    func validWordSquare(_ words: [String]) -> Bool {
    	let matrix = words.map({ Array($0.characters) })
    	guard !matrix.isEmpty, matrix.count == matrix[0].count else { return false }
    	
    	for y in matrix.indices {
    		for x in matrix[y].indices {
    			guard x < matrix.count, matrix[x].count > y else { return false }
    			
    			if matrix[y][x] != matrix[x][y] {
    				return false
    			}
    		}
    	}
    	
    	return true
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.