+ 1
Hi guys can someone help me how to solve this please?
1. Given a string, return the first recurring letter of that string. If there are no recurring letters, return âNoneâ. Input Format ⢠The input line contains a string Constraints ⢠The string length should be bigger than 2 ⢠The string should not contain whitespaces Sample Input 0 life Sample Output 0 None Explanation 0 In thw word life is no recurring letter, so the output should be None Sample Input 1 statistics Sample Output 1 t Explanation 1 The first recurring letter in the string statistics is t
2 Answers
+ 4
- Read the string
- Use String.length() method to check its length was more than 2 characters
- Iterate through the string using loop
- Use String.indexOf() to check whether there is any <i>-th character with duplicate
- Pass the index + 1 as the second argument for String.indexOf() method in order for it to start looking from a higher index. For example,
i = str.indexOf( str.charAt( i ), i + 1 );
if( i != -1 )
{
// this letter has duplicate.
}
Where <i> is an integer used as loop counter, and <str> was the input String variable.
- When String.indexOf() returns any value greater than -1, you know that character at <i>-th index has a duplicate somewhere.
(Edit)
"statistics" should yield 0 instead cause letter 's' (first character - index 0) has duplicates.
+ 1
Ipang Thank you very much for your answer :)



