www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Detect Capital

Last update: 2/22/2017 6:47:00 PM

By Fatih KABAKCI

The problem is to detect the correct using of capitals in regard to the rules which have been exposed in the following.

Problem #520 - Detect Capital

Description: Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

1. All letters in this word are capitals, like "USA".
2. All letters in this word are not capitals, like "leetcode".
3. Only the first letter in this word is capital if it has more than one letter, like "Google".

Otherwise, we define that this word doesn not use capitals in a right way.

Example 1:
Input: "USA"
Output: True

Example 2:
Input: "FlaG"
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.DetectCapital;

/**
 * @author fkabakci 
 * Problem Description: Given a word, you need to judge whether the usage of capitals in it is right or not.
 * 
 * We define the usage of capitals in a word to be right when one of the following cases holds:
 * 
 * 		1. All letters in this word are capitals, like "USA".
 * 		2. All letters in this word are not capitals, like "leetcode".
 * 		3. Only the first letter in this word is capital if it has more than one letter, like "Google".
 * 
 * Otherwise, we define that this word does not use capitals in a right way.
 * 
 * Example 1:
 * Input: "USA"
 * Output: True
 * 
 * Example 2:
 * Input: "FlaG"
 * Output: False
 * 
 * Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
 * 
 * Solution:
 * 1. Check it out if all letters are capitals.
 * 2. Check it out if all letters are not capitals.
 * 3. Check it out if word is type of camel format.
 * 4. Return combined result with shaped OR logic.
 */
public class Solution {
	public boolean detectCapitalUse(String word) {
		int length = word.length();
		
		boolean isAllUpper = true;
        for(int i = 0; i < length && isAllUpper == true; i++)
        	if(Character.isLowerCase(word.charAt(i)))
        		isAllUpper = false;
        
        boolean isAllLower = true;
        for(int i = 0; i < length && isAllLower == true; i++)
        	if(Character.isUpperCase(word.charAt(i)))
        		isAllLower = false;
        
        boolean isCamelNotation = Character.isUpperCase(word.charAt(0));
        for(int i = 1; i < length && isCamelNotation == true; i++)
        	if(Character.isUpperCase(word.charAt(i)))
        		isCamelNotation = false;
        
        return isAllUpper || isAllLower || isCamelNotation;
    }

	public static void main(String[] args) {
		Solution s = new Solution();
		System.out.println(s.detectCapitalUse("Leetcode"));
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.