C# solution with comments


  • 0
    B
    public class Solution {
      	public bool DetectCapitalUse(string word) 
      	{
      	    // Initializing instances of upper/lowercased word
      		string wordLower = word.ToLower();
      		string wordUpper = word.ToUpper();
      		
      		// Return true if all upper
      		if (word == wordUpper) { return true; }
      		// Return true if all lower
      		else if (word == wordLower) { return true; }
      		// Checking first letter capital case
      		else
      		{
      		    /*
      		        Breaking the word into a character array, and assuming the
      		        first letter is capitalized, lowercase the return and compare 
      		        to the original word. Return 'true' if properly cased, else return 'false'
      		    */
      		    
      			char[] wordChar = word.ToCharArray();
      			
      			for (int i = 1; i < wordChar.Length; i++)
      			{
      			    wordChar[i] = char.ToLower(wordChar[i]);
      			}
      			
      			string wordString = new string(wordChar);
      			if (wordString == word)
      			{
      				return true; 
      			}
      			else 
      			{
      				return false;
      			}
      		}
        }
    }
    

Log in to reply
 

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