Write a function:
class Solution { public int solution(String S); }
that, given a string S, returns the index (counting from 0) of a character such that the part of the string to the left of that character is a reversal of the part of the string to its right. The function should return −1 if no such index exists.
Note: reversing an empty string (i.e. a string whose length is zero) gives an empty string.
For example, given a string:
"racecar"
the function should return 3, because the substring to the left of the character "e" at index 3 is "rac", and the one to the right is "car".
Given a string:
"x"
the function should return 0, because both substrings are empty.
Write an efficient algorithm for the following assumptions:
- the length of string S is within the range [0..2,000,000].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
// write your code in Java SE 8
}
}
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
var mid = S.count / 2
var start = 0
var end = S.count - 1
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
var mid = S.count / 2
var start = 0
var end = S.count - 1
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
var mid = S.count / 2
var start = 0
var end = S.count - 1
let S = Array(S)
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
var mid = S.count / 2
var start = 0
var end = S.count - 1
let S = Array(S)
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
let mid = S.count / 2
var start = 0
var end = S.count - 1
let S = Array(S)
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
let mid = S.count / 2
var start = 0
var end = S.count - 1
let S = Array(S)
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ S : inout String) -> Int {
// write your code in Swift 4.2.1 (Linux)
if S.count % 2 == 0 { return -1 }
let mid = S.count / 2
var start = 0
var end = S.count - 1
let S = Array(S)
while start < mid {
if S[start] != S[end] {
return -1
}
start += 1
end -= 1
}
return mid
}
The solution obtained perfect score.