Strong Password Checker II

🏠 ⬅️ ➡️

A password is said to be strong if it satisfies all the following criteria:

  • It has at least 8 characters.
  • It contains at least one lowercase letter.
  • It contains at least one uppercase letter.
  • It contains at least one digit.
  • It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+ ".
  • It does not contain 2 of the same character in adjacent positions (i.e., "aab " violates this condition, but "aba " does not).

Given a string password, return true if it is a strong password. Otherwise, return false.

Example 1:

Input: password = "IloveLe3tcode! " Output: true Explanation: The password meets all the requirements. Therefore, we return true.

Example 2:

Input: password = "Me+You--IsMyDream " Output: false Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.

Example 3:

Input: password = "1aB! " Output: false Explanation: The password does not meet the length requirement. Therefore, we return false.

Constraints:

  • 1 <= password.length <= 100
  • password consists of letters, digits, and special characters: "!@#$%^&*()-+ ".

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i
    character(len=100) :: password

    do i = 1, size(examples)
        read(examples(i), *) password
        write(*, '(L1)') is_strong_password(password)
    end do

contains

    function is_strong_password(password) result(is_strong)
        implicit none
        character(len=*), intent(in) :: password
        logical :: is_strong

        is_strong = len(password) >= 8 &
            .and. index(password, 'abcdefghijklmnopqrstuvwxyz') /= 0 &
            .and. index(password, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') /= 0 &
            .and. index(password, '0123456789') /= 0 &
            .and. index(password, '!@#$%^&*()-+ ') /= 0 &
            .and. count(password == password(1:size(password)-1)) < 2
    end function is_strong_password

end program main

character(len=100), parameter :: examples(3) = (/ '"IloveLe3tcode! "', &
                                                  '"Me+You--IsMyDream "', &
                                                  '"1aB! "' /)
Compiled
Executed
Correct
! Declare variables
integer :: i, j, len
character(len=*) :: password

! Initialize variables
len = len_trim(password)

! Check if password is at least 8 characters long
if (len < 8) then
    write (*,*) "Password must be at least 8 characters long"
    stop
end if

! Check if password contains at least one lowercase letter
do i = 1, len
    if (password(i:i) >= 'a' .and. password(i:i) <= 'z') then
        exit
    end if
end do
if (i > len) then
    write (*,*) "Password must contain at least one lowercase letter"
    stop
end if

! Check if password contains at least one uppercase letter
do i = 1, len
    if (password(i:i) >= 'A' .and. password(i:i) <= 'Z') then
        exit
    end if
end do
if (i > len) then
    write (*,*) "Password must contain at least one uppercase letter"
    stop
end if

! Check if password contains at least one digit
do i = 1, len
    if (password(i:i) >= '0' .and. password(i:i) <= '9') then
        exit
    end if
end do
if (i > len) then
    write (*,*) "Password must contain at least one digit"
    stop
end if

! Check if password contains at least one special character
do i = 1, len
    if (password(i:i) >= '!' .and. password(i:i) <= '~') then
        exit
    end if
end do
if (i > len) then
    write (*,*) "Password must contain at least one special character"
    stop
end if

! Check if password does not contain 2 of the same character in adjacent positions
do i = 1, len - 1
    if (password(i:i) == password(i+1:i+1)) then
        write (*,*) "Password cannot contain 2 of the same character in adjacent positions"
        stop
    end if
end do

! If we reach this point, the password is strong
write (*,*) "Password is strong"

end
🌐 Data from online sources
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def merge_nodes_between_zeros(head: ListNode) -> ListNode:
    dummy = ListNode(0)
    dummy.next = head
    prev = dummy
    cur = head

    while cur:
        if cur.val == 0:
            tmp_sum = 0
            while cur.next and cur.next.val != 0:
                tmp_sum += cur.next.val
                cur = cur.next
            prev.next = ListNode(tmp_sum)
            prev = prev.next
            cur = cur.next

    return dummy.next
  1. Create a dummy node and connect it to the head of the given list.
  2. Initialize two pointers: 'prev' pointing at the dummy node, and 'cur' pointing at the head.
  3. Iterate through the list using the 'cur' pointer.
  4. When 'cur' encounters a 0 node, calculate the sum of the nodes between consecutive 0 nodes using a temporary variable (tmp_sum).
  5. Update 'prev' with a new node that contains the value of tmp_sum, update 'prev' to the new node, and move 'cur' to the next node.
  6. Repeat steps 4 and 5 until all consecutive 0 nodes are merged.
  7. Return the modified list by returning the 'next' of the dummy node, as the head of the new list.
🌐 Data from online sources
class ListNode {
public:
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};

ListNode* mergeNodesBetweenZeros(ListNode* head) {
    ListNode* dummy = new ListNode(0);
    dummy->next = head;
    ListNode* prev = dummy;
    ListNode* cur = head;

    while (cur) {
        if (cur->val == 0) {
            int tmp_sum = 0;
            while (cur->next && cur->next->val != 0) {
                tmp_sum += cur->next->val;
                cur = cur->next;
            }
            prev->next = new ListNode(tmp_sum);
            prev = prev->next;
            cur = cur->next;
        }
    }

    return dummy->next;
}
  1. Create a dummy node and connect it to the head of the given list.
  2. Initialize two pointers: 'prev' pointing at the dummy node, and 'cur' pointing at the head.
  3. Iterate through the list using the 'cur' pointer.
  4. When 'cur' encounters a 0 node, calculate the sum of the nodes between consecutive 0 nodes using a temporary variable (tmp_sum).
  5. Update 'prev' with a new node that contains the value of tmp_sum, update 'prev' to the new node, and move 'cur' to the next node.
  6. Repeat steps 4 and 5 until all consecutive 0 nodes are merged.
  7. Return the modified list by returning the 'next' of the dummy node, as the head of the new list.