Checking if a Number Reduces to Two Identical Digits
IntuitionThe problem requires checking if a given string of digits, when repeatedly transformed by replacing adjacent pairs with their sum modulo 10, eventually


## Intuition  
The problem requires checking if a given string of digits, when repeatedly transformed by replacing adjacent pairs with their sum modulo 10, eventually reduces to two identical digits.  

## Approach  
1. **Iterative Transformation**:  
   - Start with the given string `s`.  
   - Repeatedly form a new string by summing adjacent digits modulo 10.  
   - Continue this process until only two digits remain.  

2. **Final Check**:  
   - If the two remaining digits are equal, return `true`; otherwise, return `false`.  

## Complexity  
- **Time Complexity**: \(O(n^2)\), as each transformation step reduces the length by 1, leading to a total of \(O(n)\) steps, and each step requires \(O(n)\) operations.  
- **Space Complexity**: \(O(n)\), due to the storage of intermediate strings during transformation.  

## Code  
```java
class Solution {
    public boolean hasSameDigits(String s) {
        while (s.length() > 2) {
            String st = "";
            for (int i = 0; i < s.length() - 1; i++) {
                int m = Character.getNumericValue(s.charAt(i));
                int n = Character.getNumericValue(s.charAt(i + 1));
                st += ((m + n) % 10);
            }
            s = st;
        }
        return s.charAt(0) == s.charAt(1);
    }
}
```  

## Optimizations  
- **StringBuilder Instead of String Concatenation**: Using `StringBuilder` instead of `st +=` reduces time complexity from \(O(n^2)\) to \(O(n)\) per iteration.  
- **In-Place Modification**: Using an integer array instead of strings could further optimize memory usage.  

Would you like me to rewrite the code with these optimizations? 🚀
['Java']