Skip to content

Instantly share code, notes, and snippets.

@sergiorgiraldo
Created August 27, 2015 17:21
Show Gist options
  • Save sergiorgiraldo/c302933011b367647ea1 to your computer and use it in GitHub Desktop.
Save sergiorgiraldo/c302933011b367647ea1 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript">
function $(el){
return document.getElementById(el);
}
function count(){
var option = $("opt").value;
var word = $("txt").value;
if (option == "0") doIt(word.toUpperCase(),/[A-Z]/);
if (option == "1") doIt(word,/[A-Za-z]/);
}
function doIt(word,pattern){
var letters=[];
for (var i = 0; i < word.length; i++){
var letter = word.charAt(i);
if (letters.indexOf(letter) != -1) continue;
if (letter.match(pattern)){
letters.push(letter.toUpperCase());
if (i == word.length - 1 || word.indexOf(letter, i+1) == -1){
alert(letter + " at position " + (i + 1));
return;
}
}
}
}
</script>
</head>
<body>
<h2>You are given a string, find its first non-repeating character?</h2>
<input type="text" id="txt" maxlength="2000" size="100" /> <br />
What to consider <br />
<select id="opt">
<option value="0">Case insensitive</option>
<option value="1">Lowercase and uppercase letters</option>
</select><br />
<button onclick="count()">Do it</button>
</body>
</html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment