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;
}
댓글 없음:
댓글 쓰기