Is Tandem Repeat?
Description
Determine whether the given string can be obtained by one concatenation of some string to itself.
Example
- For
inputString = "tandemtandem"
, the output should beisTandemRepeat(inputString) = true
; - For
inputString = "qqq"
, the output should beisTandemRepeat(inputString) = false
; - For
inputString = "2w2ww"
, the output should beisTandemRepeat(inputString) = false
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string inputString
Guaranteed constraints:
2 ≤ inputString.length ≤ 20
. -
[output] boolean
true
ifinputString
represents a string concatenated to itself,false
otherwise.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
function isTandemRepeat(inputString) {
var l = inputString.length;
return (
l % 2 === 0 && inputString.substr(0, l / 2) === inputString.substr(l / 2)
);
}