You are given a string S, in which one letter occurs twice. Every other letter occurs at most once. Write a function: string solution(string &S); which, given a string S, returns a single-character string: the letter that occurs twice. Examples: 1. Given S = "aba", the function should return "a". 2. Given S = "zz", the function should return "z". 3. Given S = "codility", the function should return "i"..
string solution(string &S) { int letter[26] = {0}; for (int i = 0; i < S.size(); i++) { letter[S[i] - 'a']++; } for (int i = 0; i < 26; i++) { if (letter[i] > 1) return string(1, i + 'a'); } return "NONE"; }