이 블로그 검색

2023년 4월 5일 수요일

Solving Geekina Loves Order in Java

Algorithm Problem

Problem_Link

Solution(Time Complexity, Space Complexity)

O(n), O(1)


public static int validString(int N, String S) {

    // Initialize variable 'temp' to 'a' as the 
    // reference for comparison in the string.
    char temp = 'a';

    // Iterate through the length of string S.
    for (int i = 0; i < N; i++) {

        // If the i-th character of S is less than 'temp', 
	//it is not in alphabetical order, so return 0.
        if (temp > S.charAt(i)) return 0;

        // Update 'temp' to the i-th character of S.
        temp = S.charAt(i);
    }

    // If the string is in alphabetical order, return 1.
    return 1;
}

댓글 없음:

댓글 쓰기

Logic Gate Truth Tables & Definitions

Logic Gate Truth Tables Java Code !A // NOT A&B // AND ~(A&B) // NAND A|B // OR ~(A|B) // XOR A^B // XOR ~(A^B) // XNOR ~A // Inve...