Be the first user to complete this post
|
Add to List |
213. Find the last repeating character in a given string
Objective: Given a string, write an algorithm to find the last repeating character in it.
Example:
String input = "horizon tutorials" Output: 'i' String input = "algorithms" Output: No repeating character found.
Approach:
Naive approach: This problem can be easily solved using two nested loops from right to left. Take each character from the outer loop and check the character in the rest of the string using the inner loop and return the first character which is repeating. Time complexity is O(N^2).
Better approach: Using extra space
- Iterate the string from left to right.
- Count the occurrence of each character and store it on a map.
- Iterate the string again from right to left and check if the character has counted more than one in the map created in the previous step, if yes then return that character.
- If none of the characters has a count > 1 in the map, return null.
import java.util.HashMap;
public class Main {
public static Character getCharacter(String input){
//remove all the spaces
input = input.replaceAll(" ", "");
Character rptChar = null;
//Will store each character and it's count
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i <input.length(); i++) {
Character chr = input.charAt(i);
if(map.containsKey(chr)){
map.put(chr,map.get(chr)+1);
}else{
map.put(chr, 1);
}
}
//Iterate the string from end to start and return the character for which the count is > 1 in map
for (int i = input.length()-1; i >=0 ; i--) {
if(map.get(input.charAt(i))>1){
rptChar = input.charAt(i);
break;
}
}
return rptChar;
}
public static void main(String[] args) {
String input = "tutorial horizon";
Character result = getCharacter(input);
if(result!=null){
System.out.println("Last Repeating Character in '"+input+"' is: " + result);
}else{
System.out.println("No Repeating Character found");
}
}
}
def get_character(input_str):
input_str = input_str.replace(" ", "")
char_count = {}
for char in input_str:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
for char in reversed(input_str):
if char_count[char] > 1:
return char
return None
if __name__ == "__main__":
input_str = "tutorial horizon"
result = get_character(input_str)
if result:
print("Last Repeating Character in '" + input_str + "' is: " + result)
else:
print("No Repeating Character found")
package main
import (
"fmt"
"strings"
)
func getCharacter(input string) rune {
charCount := make(map[rune]int)
input = strings.ReplaceAll(input, " ", "")
for _, char := range input {
charCount[char]++
}
for i := len(input) - 1; i >= 0; i-- {
char := rune(input[i])
if charCount[char] > 1 {
return char
}
}
return 0
}
func main() {
input := "tutorial horizon"
result := getCharacter(input)
if result != 0 {
fmt.Println("Last Repeating Character in '" + input + "' is: " + string(result))
} else {
fmt.Println("No Repeating Character found")
}
}
Output:
Last Repeating Character in 'tutorial horizon' is: o