Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active March 11, 2021 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/d1643410e94506943d90acb26055e13f to your computer and use it in GitHub Desktop.
Save primaryobjects/d1643410e94506943d90acb26055e13f to your computer and use it in GitHub Desktop.
[2020-03-09] Challenge #383 [Easy] Necklace matching https://jsfiddle.net/cy0tz28q/12/
bool same_necklace(string a, string b){
if(a.size() != b.size()) return false;
a += a;
size_t index = a.find(b);
if(index != string::npos) return true;
return false;
}
const same_necklace = (necklace1, necklace2) => {
let result = false;
result = necklace1 === necklace2;
let start = 0;
if (!result && necklace1 && necklace1.length && necklace2 && necklace2.length && necklace1.length === necklace2.length) {
const ch = necklace1.charAt(0);
start = necklace2.indexOf(ch, start);
while (!result && start !== -1) {
// From the starting letter, check all letters to match to end.
let i;
let isValid = true;
for (i=start + 1; i<necklace2.length; i++) {
if (necklace2.charAt(i) !== necklace1.charAt(i - start)) {
isValid = false;
break;
}
}
if (isValid) {
// Now check from the front of necklace2 up until start.
const f = necklace1.length - start;
for (let j=0; j<start; j++) {
if (necklace2.charAt(j) !== necklace1.charAt(f+j)) {
isValid = false;
break;
}
}
}
result = isValid;
start = necklace2.indexOf(ch, start + 1);
}
}
return result;
};
// https://jsitor.com/wFlntI_MQ
const same_necklace = (str1, str2) => {
// Check if the strings are equal.
let result = str1 === str2;
// Check if the strings are equal in length.
if (str1.length === str2.length) {
// Find first occurrence of start of str1 within str2.
let index2 = str2.indexOf(str1[0]);
// Remember our starting index in str2, as we will continue searching from here.
let start = index2;
// Check if we found a new match for the starting index in str2, from the first character in str1.
while (index2 !== -1) {
// Check if each letter in str1 matches from the starting index in str2.
for (index1 = 0; index1 < str1.length; index1++) {
// Check if letter match.
if (str2[index2++] !== str1[index1]) {
// No match, continue to next possible match or return false.
break;
}
else if (index2 >= str2.length) {
// Wrap to beginning of str2 and continue matching.
index2 = 0;
}
}
if (index1 === str1.length && str2[index2 - 1] === str1[str1.length - 1]) {
// We've matched all characters successfully.
result = true;
break;
}
// Find next matching character of beginning of str1 in str2.
index2 = str2.indexOf(str1[0], start + 1);
// Remember our next position in str2, as we will continue searching from here.
start = index2;
}
}
return result;
};
$(function() {
const output = $('#output');
print(same_necklace('nicole', 'icolen') || 'false');
print(same_necklace("nicole", "lenico") || 'false');
print(same_necklace("nicole", "coneli") || 'false');
print(same_necklace("aabaaaaabaab", "aabaabaabaaa") || 'false');
print(same_necklace("abc", "cba") || 'false');
print(same_necklace("xxyyy", "xxxyy") || 'false');
print(same_necklace("xyxxz", "xxyxz") || 'false');
print(same_necklace("x", "x") || 'false');
print(same_necklace("x", "xx") || 'false');
print(same_necklace("x", "") || 'false');
print(same_necklace("", "") || 'false');
});
const print = str => {
$('#output').append(str + '<br>');
}
Source: https://www.reddit.com/r/dailyprogrammer/comments/ffxabb/20200309_challenge_383_easy_necklace_matching/
same_necklace("nicole", "icolen") => true
same_necklace("nicole", "lenico") => true
same_necklace("nicole", "coneli") => false
same_necklace("aabaaaaabaab", "aabaabaabaaa") => true
same_necklace("abc", "cba") => false
same_necklace("xxyyy", "xxxyy") => false
same_necklace("xyxxz", "xxyxz") => false
same_necklace("x", "x") => true
same_necklace("x", "xx") => false
same_necklace("x", "") => false
same_necklace("", "") => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment