Problem

Given a string, find the last character (from right) that appears more than once in the string (ignoring spaces). If no such character exists, return a suitable message.

Examples

Example 1

1
2
3
4
Input:
str = "horizon tutorials"
Output: 'i'
Explanation: 'i' is the last character (from right) that appears more than once.

Example 2

1
2
3
4
Input:
str = "algorithms"
Output: No repeating character found.
Explanation: Every character is unique.

Solution

Method 1 – Naive Double Loop

Intuition

Check each character from right to left, and for each, scan the rest of the string to see if it appears more than once.

Approach

  1. Iterate the string from right to left.
  2. For each character, check if it appears elsewhere in the string.
  3. Return the first such character found.
  4. If none found, return a suitable message.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
  char lastRepeatingChar(const string& s) {
    for (int i = s.size() - 1; i >= 0; --i) {
      if (s[i] == ' ') continue;
      for (int j = 0; j < s.size(); ++j) {
        if (i != j && s[i] == s[j]) return s[i];
      }
    }
    return '\0'; // or throw/return special value
  }
};
1
2
3
4
5
6
7
8
9
func lastRepeatingChar(s string) byte {
  for i := len(s) - 1; i >= 0; i-- {
    if s[i] == ' ' { continue }
    for j := 0; j < len(s); j++ {
      if i != j && s[i] == s[j] { return s[i] }
    }
  }
  return 0
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public Character lastRepeatingChar(String s) {
    for (int i = s.length() - 1; i >= 0; i--) {
      if (s.charAt(i) == ' ') continue;
      for (int j = 0; j < s.length(); j++) {
        if (i != j && s.charAt(i) == s.charAt(j)) return s.charAt(i);
      }
    }
    return null;
  }
}
1
2
3
4
5
6
7
class Solution:
  def last_repeating_char(self, s: str) -> str | None:
    for i in range(len(s) - 1, -1, -1):
      if s[i] == ' ': continue
      if s.count(s[i]) > 1:
        return s[i]
    return None

Complexity

  • ⏰ Time complexity: O(n^2), since for each character, we may scan the whole string.
  • 🧺 Space complexity: O(1), no extra space beyond variables.

Method 2 – Hash Map for Counting

Intuition

Count the occurrences of each character, then scan from right to left to find the last character with count > 1.

Approach

  1. Remove spaces from the string.
  2. Count occurrences of each character using a hash map.
  3. Iterate from right to left, return the first character with count > 1.
  4. If none found, return a suitable message.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <unordered_map>
class Solution {
public:
  char lastRepeatingChar(const string& s) {
    unordered_map<char, int> cnt;
    for (char c : s) if (c != ' ') cnt[c]++;
    for (int i = s.size() - 1; i >= 0; --i) {
      if (s[i] != ' ' && cnt[s[i]] > 1) return s[i];
    }
    return '\0';
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func lastRepeatingChar(s string) byte {
  cnt := map[byte]int{}
  for i := 0; i < len(s); i++ {
    if s[i] != ' ' { cnt[s[i]]++ }
  }
  for i := len(s) - 1; i >= 0; i-- {
    if s[i] != ' ' && cnt[s[i]] > 1 { return s[i] }
  }
  return 0
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import java.util.*;
class Solution {
  public Character lastRepeatingChar(String s) {
    Map<Character, Integer> cnt = new HashMap<>();
    for (char c : s.toCharArray()) if (c != ' ') cnt.put(c, cnt.getOrDefault(c, 0) + 1);
    for (int i = s.length() - 1; i >= 0; i--) {
      if (s.charAt(i) != ' ' && cnt.get(s.charAt(i)) > 1) return s.charAt(i);
    }
    return null;
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def last_repeating_char(self, s: str) -> str | None:
    from collections import Counter
    cnt = Counter(c for c in s if c != ' ')
    for i in range(len(s) - 1, -1, -1):
      if s[i] != ' ' and cnt[s[i]] > 1:
        return s[i]
    return None

Complexity

  • ⏰ Time complexity: O(n), since we scan the string twice.
  • 🧺 Space complexity: O(1) (if alphabet is fixed), otherwise O(n) for the hash map.