✅2 Method's ||Strings||🌟JAVA||🧑‍💻 BEGINNER FREINDLY||C++||Python
Approach1: Brute Force1.Loop Until Two Digits:  Use a loop to keep processing the string until its length is reduced to 2.
2.Calculate New Digits: For each pair


# Approach1: Brute Force
<!-- Describe your approach to solving the problem. -->
1.**Loop Until Two Digits:**  Use a loop to keep processing the string until its length is reduced to ```2```.
2.**Calculate New Digits:** For each pair of consecutive digits, calculate the new digit as ```(firstDigit + secondDigit) % 10```.
3.**Build New String**:Construct a new string from the calculated digits.
4.**Check Final Digits:** Once the loop ends, check if the two remaining digits are the same.
____
# ⏳Complexity Analysis
- Time complexity:🕒 $$O(n)$$ in each iteration
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(n) $$for the new string being built.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
____


# 💻Code Implementation
```java []
class Solution {
    public boolean hasSameDigits(String s) {
           while (s.length() > 2) {
            StringBuilder newbornString = new StringBuilder();
            for (int i = 0; i < s.length() - 1; i++) {
                int firstDigit = s.charAt(i) - '0';               // Convert char to int
                int secondDigit = s.charAt(i + 1) - '0';          // Convert char to int
                int newDigit = (firstDigit + secondDigit) % 10;   // Calculate new digit
                newbornString.append(newDigit);                   // Append new digit to the new string
            }
            s = newbornString.toString();                         // Update s to the new string
        }
        // Check if the final two digits are the same
        return s.charAt(0) == s.charAt(1);
    }
    
}
```
```c++ []
class Solution {
public:
    bool hasSameDigits(std::string s) {
        while (s.length() > 2) {
            std::string newbornString;
            for (size_t i = 0; i < s.length() - 1; i++) {
                int firstDigit = s[i] - '0';               // Convert char to int
                int secondDigit = s[i + 1] - '0';          // Convert char to int
                int newDigit = (firstDigit + secondDigit) % 10; // Calculate new digit
                newbornString += std::to_string(newDigit); // Append new digit to the new string
            }
            s = newbornString;                             // Update s to the new string
        }
        // Check if the final two digits are the same
        return s[0] == s[1];
    }
};
```
```python []
class Solution(object):
    def hasSameDigits(self, s):
        while len(s) > 2:
            newborn_string = ""
            for i in range(len(s) - 1):
                first_digit = int(s[i])               # Convert char to int
                second_digit = int(s[i + 1])          # Convert char to int
                new_digit = (first_digit + second_digit) % 10  # Calculate new digit
                newborn_string += str(new_digit)      # Append new digit to the new string
            s = newborn_string                        # Update s to the new string
        # Check if the final two digits are the same
        return s[0] == s[1]
 ```
___
# Approach2:HashSet

1 . **Unique Digits Check:**

-  We start by creating a ```HashSet``` to store unique digits from the input string ```s```.
-  If the string only contains one unique digit, return ```true``` because all digits are the same.

 2 . **Iterative Reduction:**

-  If the string has more than one unique digit, we reduce the string by summing adjacent digits modulo 10 until the length of the string becomes 2 or fewer.

3 . **Final Check:**

-  Once the string is reduced to 2 digits, check if both digits are the same.
-  Return ```true``` if they are the same; otherwise, return 
```false```.
___

# ⏳Complexity Analysis
- Time complexity:🕒 $$O(n²)$$ 
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(n) $$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
___

### 💻Code Implementation

```java []
class Solution {
    public boolean hasSameDigits(String s) {
        HashSet<Character> uniqueDigits = new HashSet<>();
        
        // Add all digits to the HashSet
        for (int i = 0; i < s.length(); i++) {
            uniqueDigits.add(s.charAt(i));
        }
        
        // If there's only one unique digit, return true
        if (uniqueDigits.size() == 1) {
            return true;
        }
        
        // Reduce the digits until we have two or fewer
        while (s.length() > 2) {
            StringBuilder newbornString = new StringBuilder();
            for (int i = 0; i < s.length() - 1; i++) {
                // Calculate new digit as the sum of adjacent digits modulo 10
                int newDigit = (s.charAt(i) - '0' + s.charAt(i + 1) - '0') % 10;
                newbornString.append(newDigit);
            }
            s = newbornString.toString(); // Update s to the new string
        }
        
        // Check if the final two digits are the same
        return s.charAt(0) == s.charAt(1);
    
    }
    
}
```
```c++ []
class Solution {
public:
    bool hasSameDigits(std::string s) {
        std::unordered_set<char> uniqueDigits;
        
        // Add all digits to the unordered_set
        for (int i = 0; i < s.length(); i++) {
            uniqueDigits.insert(s[i]);
        }
        
        // If there's only one unique digit, return true
        if (uniqueDigits.size() == 1) {
            return true;
        }
        
        // Reduce the digits until we have two or fewer
        while (s.length() > 2) {
            std::string newbornString;
            for (int i = 0; i < s.length() - 1; i++) {
                // Calculate new digit as the sum of adjacent digits modulo 10
                int newDigit = (s[i] - '0' + s[i + 1] - '0') % 10;
                newbornString += std::to_string(newDigit);
            }
            s = newbornString; // Update s to the new string
        }
        
        // Check if the final two digits are the same
        return s[0] == s[1];
    }
};

```
```python []
class Solution(object):
    def hasSameDigits(self, s):
        unique_digits = set(s)
        
        # If there's only one unique digit, return true
        if len(unique_digits) == 1:
            return True
        
        # Reduce the digits until we have two or fewer
        while len(s) > 2:
            newborn_string = ""
            for i in range(len(s) - 1):
                # Calculate new digit as the sum of adjacent digits modulo 10
                new_digit = (int(s[i]) + int(s[i + 1])) % 10
                newborn_string += str(new_digit)
            s = newborn_string  # Update s to the new string
        
        # Check if the final two digits are the same
        return s[0] == s[1]
```
**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**

**🔼 Please Upvote**
**🔼 Please Upvote**
**🔼 Please Upvote**

![4475115f-1911-4a0f-9c2d-4f384651be06_1737864862.4057631.png](https://assets.leetcode.com/users/images/27471bfe-db04-4920-8da0-224358ca0c46_1740284441.741556.png)


['Hash Table', 'String', 'Python', 'C++', 'Java']