Skip to main content

Posts

Leetcode 69: Sqrt(x)

1. Problem Statement (Simple Explanation) You’re given a  non-negative integer  x. You must return: ⌊√x⌋ — the square root of x rounded  down  to the nearest integer. The result must be  non-negative . You  must not  use built-in exponent or sqrt functions (pow, sqrt, x**0.5, etc.). 2. Examples Example 1: Input: x = 4 √4 = 2 exactly. Output: 2 Example 2: Input: x = 8 √8 ≈ 2.828..., floor is 2. Output: 2 3. Approach – Binary Search on Answer (O(log x)) We want to find the  largest integer m such that : m * m <= x Because x is up to  2 31 - 1, m will be at most 46340 (since 46340 2 < 2 31 -1, but  46341 2  overflows 32-bit int). But we can be general. We can binary search on m in range [0, x]: Let left = 0, right = x. While left <= right: mid = left + (right - left) / 2. Compute mid * mid (use 64-bit / lo...

Lapindromes

A lapindrome is a string that, when split in the middle, gives two halves having: The same characters With the same frequency of each character If the string length is odd , the middle character is ignored . Examples of lapindromes: gaga → ga and ga abccab → abc and cab (same multiset of characters) rotor → ro and or (ignore middle t) xyzxy → xy and xy Non-examples: abbaab → halves abb and aab → same characters but frequencies differ. abcde → halves ab and de → different characters. Your task: Given a string, check if it is a lapindrome. Problem Statement: For each test case: You are given a string S (only lowercase English letters). Determine whether S is a lapindrome . Output: Print "YES" if it is a lapindrome. Print "NO" otherwise. Input Format: First line: integer T — number of test cases. Next T lines: each line contains a string S. Output Format: For each test case, print YES or NO on its own line. Constraints: 1 ≤ T ≤ 100 2 ≤ ∣S∣ ≤1000 S consists only of lo...